//--------------------------------------------------------------------//
// Do-file: 03_Tables 
//
// Paper: "Little evidence that military policing reduces crime 
// or improves human security"
//
// Authors: Robert Blair and Michael Weintraub
//
// Date created: 2022-06-30
// Date last modified:2023-01-06
// 
// Notes:
// (1) This code builds the tables included in the main section and the appendix
// of the paper. Each figures includes the randomization inference p-value. To 
// calculate it, each regression is run 10,000 times with different simulations 
// of the randomization process. 
//
//--------------------------------------------------------------------//


					///////////////////////////////////////////
					///								   		///
					///		Tables for the main section		///
					///				of the paper			///
					///								   		///
					///////////////////////////////////////////

					
//---------//
// -- Preliminaries 
//---------//

clear all
set more off
graph set window fontface "Times New Roman"


* Defining main computer path
global path "~/Dropbox/Asistencia de investigacion/Proyecto Cali/Mapas Cali/08_Data&Analysis/02_dofiles/v20220630_PaperReplication" 

		
* Set working directory
cd "${path}" 

	
* Uploading housekeeping
include "${path}/Code/01_Housekeeping.do"
	
		
* Increase max number of variables 
set maxvar 20000


* Program to put checkmark in the latex tables

	cap program drop checkmark_yes
	program define checkmark_yes
		syntax, outcomes(string) mat1t(string) mat1s(string) mat2t(string) ///
								  mat2s(string) mat3t(string) mat3s(string) ename(string)
		
		loc n = `: word count `outcomes''	
		forvalues j = 1/`n' {
			
			estadd scalar qval = `mat1t'[`j',2], :`ename'`j'
			estadd scalar qval2 = `mat1s'[`j',2], :`ename'`j'
			
			loc qval3 = `mat2t'[`j',4]
				if `qval3' == 0 loc t ""
				if `qval3' == 1 loc t "Yes"
					estadd local text "`t'", :`ename'`j'
				
			loc qval4 = `mat2s'[`j',4]
				if `qval4' == 0 loc t ""
				if `qval4' == 1 loc t "Yes"
					estadd local text1 "`t'", :`ename'`j'
	
			loc qval5 = `mat3t'[`j',5]
				if `qval5' == 0 loc t ""
				if `qval5' == 1 loc t "Yes"
					estadd local text2 "`t'", :`ename'`j'

			loc qval6 = `mat3s'[`j',5]
				if `qval6' == 0 loc t ""
				if `qval6' == 1 loc t "Yes"
					estadd local text3 "`t'", :`ename'`j'		
		}

	end

	
* Programs to perform the Benjamini-Hochberg (1995) correction, based on the 
* methodology proposed by Anderson (2008)

	* Benjamini-Hochberg (1995) - qvalues
		cap program drop bhcorr
		program define bhcorr 
			syntax, mat(string) newname(string) outmat(string)
			
			preserve 
			
				* Prepare matrix
				drop _all
				svmat `mat'
				
				*-------------------------------------
				// Part 1 of the program: store rownames and create a variable with the names
				*-------------------------------------
				
				* Store rownames
				loc rownames: rowfullnames `mat'
				loc c: word count `rownames'
				
				* Rename var and create rownames var
				rename `mat'1 `newname'
				gen rownames = ""
				
				* Replace each var name
				forvalues i = 1/`c' {
					replace rownames = "`: word `i' of `rownames''" in `i'
				}
				
				*---------------------------------------------------------------------
				// Part 2: Apply BH(1995) correction. Anderson (2008) methodology.
				*---------------------------------------------------------------------
				qui sum `newname'
				loc totalpvals = r(N)
		
				* Sort the p-values in ascending order and generate a variable that codes each p-value's rank
				qui gen int original_sorting_order = _n
				qui sort `newname'
				qui gen int rank = _n if !missing(`newname')
						
				* Set the initial counter to 1 
				local qval = 1

				* Generate the variable that will contain the BH (1995) q-values
				gen bh95_qval = 1 if !missing(`newname')

				* Set up a loop that begins by checking which hypotheses are rejected at q = 1.000, 
				* then checks which hypotheses are rejected at q = 0.999, then checks which hypotheses 
				* are rejected at q = 0.998, etc.  The loop ends by checking which hypotheses are 
				* rejected at q = 0.001.

				while `qval' > 0 {
				
					* Generate value qr/M
					qui gen fdr_temp = `qval'*rank/`totalpvals'
					
					* Generate binary variable checking condition p(r) <= qr/M
					qui gen reject_temp = (fdr_temp >= `newname') if !missing(fdr_temp)
					
					* Generate variable containing p-value ranks for all p-values that meet above condition
					qui gen reject_rank = reject_temp*rank
					
					* Record the rank of the largest p-value that meets above condition
					qui egen total_rejected = max(reject_rank)
					
					* A p-value has been rejected at level q if its rank is less than or equal to the rank 
					* of the max p-value that meets the above condition
					replace bh95_qval = `qval' if rank <= total_rejected & !missing(rank)
					
					* Reduce q by 0.001 and repeat loop
					quietly drop fdr_temp reject_temp reject_rank total_rejected
					local qval = `qval' - .001
				}
			
				quietly sort original_sorting_order
				display "Code has completed."
				display "Benjamini Hochberg (1995) q-vals are in variable 'bh95_qval'"
				display	"Sorting order is the same as the original vector of p-values"

				* Store as matrix
				mkmat `newname' bh95 original rank, mat(`outmat') rownames(rownames)
			
			restore
		end	
	
	* Benjamini-Hochberg (1995) - rejected at q level.
		cap program drop bhcorralt
		program define bhcorralt 
			syntax, mat(string) newname(string) fdr(real) outmat(string)
			
			preserve 
			
				* Prepare matrix
				drop _all
				svmat `mat'
				
				* Store rownames
				loc rownames: rowfullnames `mat'
				loc c: word count `rownames'
				
				* Rename var and create rownames var
				rename `mat'1 `newname'
				gen rownames = ""
				
				* Replace each var name
				forvalues i = 1/`c' {
					replace rownames = "`: word `i' of `rownames''" in `i'
				}
				
				* Store number of hypotheses
				qui sum `newname'
				loc totalpvals = r(N)
		
				* Sort the p-values in ascending order and generate a variable that codes each p-value's rank
				qui gen int original_sorting_order = _n
				qui sort `newname'
				qui gen int rank = _n if !missing(`newname')
						
				* Generate the variable that will contain the BH (1995) q-values
				gen bh95_cval = (rank*`fdr')/`totalpvals'  if !missing(`newname')
				
				gen ind_bh = (`newname' < bh)
					summ rank if ind == 1
					
					if `r(N)' > 0 {
						replace ind = 1 if rank <= `r(max)' & ind == 0
					}
					
				qui sort original_sorting_order
				display "Code has completed."
				display "Benjamini Hochberg (1995) q-vals are in variable 'bh95_qval'"
				display	"Sorting order is the same as the original vector of p-values"

				* Store as matrix
				mkmat `newname' original rank ind_bh, mat(`outmat') rownames(rownames)
			
			restore
		end	
		
	* Holms
		cap program drop holmscorr
		program define holmscorr 
			syntax, mat(string) newname(string) fwer(real) outmat(string)
			
			preserve 
			
				* Prepare matrix
				drop _all
				svmat `mat'
				
				* Store rownames
				loc rownames: rowfullnames `mat'
				loc c: word count `rownames'
				
				* Rename var and create rownames var
				rename `mat'1 `newname'
				gen rownames = ""
				
				* Replace each var name
				forvalues i = 1/`c' {
					replace rownames = "`: word `i' of `rownames''" in `i'
				}
				
				* Store number of hypotheses
				qui sum `newname'
				loc N = r(N)
		
				* Sort the p-values in ascending order and generate a variable that codes each p-value's rank
				qui gen int original_sorting_order = _n
				qui sort `newname'
				qui gen int rank = _n if !missing(`newname')
						
				* Generate the variable that will contain the Holms (1979) correction value
				gen hb 	   = `fwer'/(`N' - rank + 1)
				gen hb_ind = (`newname' < hb)
				qui sort original_sorting_order
				
				display "Code has completed."
				display "Holms corrected pvalue are in variable hb"
				display	"Sorting order is the same as the original vector of p-values"

				* Store as matrix
				mkmat `newname' original rank hb hb_ind, mat(`outmat') rownames(rownames)
			
			restore
		end	
		
		
		
		//--------------------------------------------------------------//
		//-    Table 1. Randomized military patrols had little to no   -//
		//	  effect on crime while the intervention was ongoing, and   //
		//	  had adverse effects after the intervention was complete	// 
		//--------------------------------------------------------------//	

//-- Column 1 --//
	
	//- Load data admin data (during the intervention)
	
	use "$data/raw/admin_data_during.dta", clear
	
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	global blockdemovars block_age block_educ block_pct_male

	
	//- Run regression with crime during intervention 
	
	reg unw_crime2_num  i.treatment /// 
		i.barrio_code $geovars $blockdemovars cum_all_unw_crime2_num [pweight=iweight]
	
	qui sum unw_crime2_num if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store UCP_noInt
	
	
//-- Column 2 --//

	//- Load admin data (after the intervention)
	
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	global blockdemovars block_age block_educ block_pct_male
				
	use "$data/raw/admin_data_after.dta", clear
	

	//- Run regression with crime after intervention 

	reg unw_crime_num  i.treatment /// 
		i.barrio_code $geovars $blockdemovars cum_all_unw_crime_num [pweight=iweight]
	
	qui sum unw_crime_num if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store AdminShort2020_unw

	
//-- Columns 3, 4 y 5--//

	//- Load data of the endline survey

	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 

	
	//- Crime victimization during intervention

	reg i2_victimduringindex_std  i.treatment /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i2_victimduringindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store c_dur

					 
	//- Crime victimization  after intervention
	
	reg i2_victimafterindex_std  i.treatment /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i2_victimafterindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store c_pos
	

	//- Crime witnessing after intervention 

	reg i_witnessindex_std  i.treatment /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_witnessindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store w_pos
	
					 				
						//---------------------------------//
						//-  Run randomization inference  -//
						//---------------------------------//
						
//------//
//- Simulate table 1, column 1 - 100,000 times 
//------//

* Define globals for regressions
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	global blockdemovars block_age block_educ block_pct_male


* Set number of simulations per group (there are 10,000 treatment variables in each one)
	local nsimul 10000
		
	
* Run regressions

forvalues p = 1/10{
	
	di "*** THIS IS SIMULATION BATCH NUMBER `p' ***"
	
	* Create empty matrix
	matrix treat_effects = J(`nsimul', 2, .)
	matrix colnames treat_effects = treat_ef spillover_ef
	
	
	* Create locals for first and last simulation of each batch
	local first = 1 + 10000*(`p'-1)
	local last = `nsimul'+ 10000*(`p'-1)
	
	di "** First simulation in batch is: `first'"
	di "** Last simulation in batch is: `last'"

	
	forvalues n = `first'/`last'{
	
			use "$data/rand_inf/block_simulate_randomizations_p`p'.dta", clear
				
			di "This is simulation number `n'"
			
		
	//- Obtain treatment status of each block 
			keep manzana treatment_ri_`n'
			rename treatment_ri_`n' treatment_ri
			
	 
	//- Merge with outcomes
			merge 1:1 manzana using "$data/raw/admin_data_during.dta"
				drop _merge
			
		
		//- Run regressions 
		
			qui: reg unw_crime2_num  i.treatment_ri /// 
			i.barrio_code $geovars $blockdemovars cum_all_unw_crime2_num  [pweight=iweight]
			
			
			* Store coefficients in matrix
			matrix treat_effects[(`n'-10000*(`p'-1)),1] = _b[1.treatment_ri]
			matrix treat_effects[(`n'-10000*(`p'-1)),2] = _b[2.treatment_ri]
}

* Turn ATEs into variables
	
	clear	
	
	svmat treat_effects, names(col)
	
	save "$data/rand_inf/RI_table1_col1_p`p'.dta", replace
	
}


	
//------//
//- Simulate table 1, column 2 - 100,000 times 
//------//

* Define globals for regressions
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	global blockdemovars block_age block_educ block_pct_male


* Set number of simulations per group 
	local nsimul 10000
	
	
* Run regressions

forvalues p = 1/10{
	
	di "*** THIS IS SIMULATION BATCH NUMBER `p' ***"
	
	* Create empty matrix
	matrix treat_effects = J(`nsimul', 2, .)
	matrix colnames treat_effects = treat_ef spillover_ef
	
	
	* Create locals for first and last simulation of each batch
	local first = 1 + 10000*(`p'-1)
	local last = `nsimul'+ 10000*(`p'-1)
	
	di "** First simulation in batch is: `first'"
	di "** Last simulation in batch is: `last'"

	
	forvalues n = `first'/`last'{
	
			use "$data/rand_inf/block_simulate_randomizations_p`p'.dta", clear
		
			di "This is simulation number `n'"
	
	
	//- Obtain treatment status of each block 
			keep manzana treatment_ri_`n'
			rename treatment_ri_`n' treatment_ri
			
	 
	//- Merge with outcomes
			merge 1:1 manzana using "$data/raw/admin_data_after.dta"
				drop _merge
			
		//- Run regressions 
		
			qui: reg unw_crime_num  i.treatment_ri /// 
			i.barrio_code $geovars $blockdemovars cum_all_unw_crime_num  [pweight=iweight]
			
			
			* Store coefficients in matrix
			matrix treat_effects[(`n'-10000*(`p'-1)),1] = _b[1.treatment_ri]
			matrix treat_effects[(`n'-10000*(`p'-1)),2] = _b[2.treatment_ri]
}

* Turn ATEs into variables
	
	clear	
	
	svmat treat_effects, names(col)
	
	save "$data/rand_inf/RI_table1_col2_p`p'.dta", replace
	
}

	
	
	
//------//
//- Simulate table 1, column 3 - 100,000 times 
//------//

* Define globals for regressions
	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 


* Set number of simulations per group 
	local nsimul 10000

	
* Run regressions

forvalues p = 1/10{
	
	di "*** THIS IS SIMULATION BATCH NUMBER `p' ***"
	
	* Create empty matrix
	matrix treat_effects = J(`nsimul', 2, .)
	matrix colnames treat_effects = treat_ef spillover_ef
	
	
	* Create locals for first and last simulation of each batch
	local first = 1 + 10000*(`p'-1)
	local last = `nsimul'+ 10000*(`p'-1)
	
	di "** First simulation in batch is: `first'"
	di "** Last simulation in batch is: `last'"

	
	forvalues n = `first'/`last'{
	
			use "$data/rand_inf/block_simulate_randomizations_p`p'.dta", clear
		
			di "This is simulation number `n'"
	
	
	//- Obtain treatment status of each block 
			keep manzana treatment_ri_`n'
			rename treatment_ri_`n' treatment_ri
			rename manzana manzana_code
			
	 
	//- Merge with outcomes
			merge 1:m manzana_code using "$data/raw/survey_endline.dta"
				drop _merge
		
		
	//- Run regressions 
		
			qui: reg i2_victimduringindex_std  i.treatment_ri /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
			
			
			* Store coefficients in matrix
			matrix treat_effects[(`n'-10000*(`p'-1)),1] = _b[1.treatment_ri]
			matrix treat_effects[(`n'-10000*(`p'-1)),2] = _b[2.treatment_ri]
}

* Turn ATEs into variables
	
	clear	
	
	svmat treat_effects, names(col)
	
	save "$data/rand_inf/RI_table1_col3_p`p'.dta"
	
}

		

	
//------//
//- Simulate table 1, column 4 - 100,000 times 
//------//

* Define globals for regressions
	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 


* Set number of simulations per group 
	local nsimul 10000

	
* Run regressions

forvalues p = 1/10{
	
	di "*** THIS IS SIMULATION BATCH NUMBER `p' ***"
	
	* Create empty matrix
	matrix treat_effects = J(`nsimul', 2, .)
	matrix colnames treat_effects = treat_ef spillover_ef
	
	
	* Create locals for first and last simulation of each batch
	local first = 1 + 10000*(`p'-1)
	local last = `nsimul'+ 10000*(`p'-1)
	
	di "** First simulation in batch is: `first'"
	di "** Last simulation in batch is: `last'"

	
	forvalues n = `first'/`last'{
	
			use "$data/rand_inf/block_simulate_randomizations_p`p'.dta", clear
		
			di "This is simulation number `n'"
	
	
	//- Obtain treatment status of each block 
			keep manzana treatment_ri_`n'
			rename treatment_ri_`n' treatment_ri
			rename manzana manzana_code
			
	 
	//- Merge with outcomes
			merge 1:m manzana_code using "$data/raw/survey_endline.dta"
				drop _merge
		
		
	//- Run regressions 
		
			qui: reg i2_victimafterindex_std  i.treatment_ri /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
			
			
			* Store coefficients in matrix
			matrix treat_effects[(`n'-10000*(`p'-1)),1] = _b[1.treatment_ri]
			matrix treat_effects[(`n'-10000*(`p'-1)),2] = _b[2.treatment_ri]
}

* Turn ATEs into variables
	
	clear	
	
	svmat treat_effects, names(col)
	
	save "$data/rand_inf/RI_table1_col4_p`p'.dta"
	
}

		
		
//------//
//- Simulate table 1, column 5 - 100,000 times 
//------//

* Define globals for regressions
	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 


* Set number of simulations per group 
	local nsimul 10000

	
* Run regressions

forvalues p = 1/10{
	
	di "*** THIS IS SIMULATION BATCH NUMBER `p' ***"
	
	* Create empty matrix
	matrix treat_effects = J(`nsimul', 2, .)
	matrix colnames treat_effects = treat_ef spillover_ef
	
	
	* Create locals for first and last simulation of each batch
	local first = 1 + 10000*(`p'-1)
	local last = `nsimul'+ 10000*(`p'-1)
	
	di "** First simulation in batch is: `first'"
	di "** Last simulation in batch is: `last'"

	
	forvalues n = `first'/`last'{
	
			use "$data/rand_inf/block_simulate_randomizations_p`p'.dta", clear
		
			di "This is simulation number `n'"
	
	
	//- Obtain treatment status of each block 
			keep manzana treatment_ri_`n'
			rename treatment_ri_`n' treatment_ri
			rename manzana manzana_code
			
	 
	//- Merge with outcomes
			merge 1:m manzana_code using "$data/raw/survey_endline.dta"
				drop _merge
		
		
	//- Run regressions 
		
			qui: reg i_witnessind i.treatment_ri /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
			
			
			* Store coefficients in matrix
			matrix treat_effects[(`n'-10000*(`p'-1)),1] = _b[1.treatment_ri]
			matrix treat_effects[(`n'-10000*(`p'-1)),2] = _b[2.treatment_ri]
}

* Turn ATEs into variables
	
	clear	
	
	svmat treat_effects, names(col)
	
	save "$data/rand_inf/RI_table1_col5_p`p'.dta"
	
}

	
	
//------//					 
//-- Store p-values from randomization inference
//------//

foreach n of numlist 1/5{
	
	* Load data of simulations
	
	di "*** THIS IS COLUMN NUMBER `n' ***"
	
	use "$data/rand_inf/RI_table1_col`n'_p1.dta", clear

	forvalues p =2/10{
		di "* This is simulation batch number `p' *"
		append using "$data/rand_inf/RI_table1_col`n'_p`p'.dta"
	}
	
	
	* Restore estimates of each regression
	if `n' == 1 {
		estimates restore UCP_noInt
	} 
	
	if `n' == 2 {
		estimates restore AdminShort2020_unw
	} 
	
	if `n' == 3 {
		estimates restore c_dur
	}
	
	if `n' == 4 {
		estimates restore c_pos
	}
	
	if `n' == 5 {
		estimates restore w_pos
	}
	

	* P-value of treatment efffect

	count if abs(treat_ef) > abs(_b[1.treatment])
	estadd scalar p_val_treat =  `r(N)'/100000, replace

	
	* P-value of spillover effect

	count if abs(spillover_ef) > abs(_b[2.treatment])
	estadd scalar p_val_spillover =  `r(N)'/100000, replace
} 

	
	
//------//	
//-- Export regressions - Nature Human Behavior Format 
//------//				
				
	loc var1 `"\shortstack{During \\ intervention}"'
	loc var2 `"\shortstack{After \\ intervention}"'
	loc var3 `"\shortstack{During \\ intervention}"'
	loc var4 `"\shortstack{After \\ intervention}"'
	loc var5 `"\shortstack{After \\ intervention}"'
				
	esttab UCP_noInt AdminShort2020_unw c_dur c_pos w_pos ///
		using "$tables\table1.tex", ///
		legend booktabs f replace ///
		order(1.treatment 2.treatment) compress ///
		keep(1.treatment 2.treatment) /// 
		indicate("Individual controls = ${demovars}" "Neighborhood FE = *.barrio_code*" "Block-level controls = ${geovars}" , labels("Yes" "No")) ///
		stats(N r2 c_mean p_val_treat p_val_spillover, fmt(0 2 3 3 3)  labels("Observations" "\$R^2$" "Control mean" "RI p-value (treatment)" "RI p-value (spillover)")) ///
		nostar	///
		cells("b(fmt(3))" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonotes nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var3'"' `"`var4'"' `"`var5'"')  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   &  \multicolumn{2}{c}{\textbf{Admin data}} &  \multicolumn{3}{c}{\textbf{Endline survey}} \\ ///
		   \cline{2-3} \cline{4-6} 							///
		   &  \multicolumn{2}{c}{\textbf{Crime incidence}} & 				///
			  \multicolumn{2}{c}{\textbf{Crime victimization}} &			///
			  \multicolumn{1}{c}{\textbf{Crime witnessing}} \\			///
			  \cline{2-3} \cline{4-5} \cline{6-6}								///		   
	   \noalign{\smallskip}											///
	   )  															/// 
	   posthead(\hline ) ///
	   postfoot(  													///
	   		  \noalign{\smallskip} \hline 					///
		  \end{tabular} 											///
		  \medskip     												///
	   ) 	   
					


					///////////////////////////////////////////
					///								   		///
					///		Tables for the appendix 		///
					///				of the paper			///
					///								   		///
					///////////////////////////////////////////

				//--------------------------------------------------//
				//-- Table A.2: Descriptive statistics on patrols --//
				//--------------------------------------------------//
						
//----//
//- Data on patrols at the block level - from the monitoring data 
//----//
	
//- First part: Average statistics of patrols per block 
	
	use "$data/raw/patrols_block_data.dta", clear
		
	* Set vars
		global outcomes mnz_visit_count mnz_duration_patrol_avg n_patrollers_avg mnz_day_correct_avg
		
	* Define matrix
		loc r = `:word count $outcomes'
		mat de = J(`r',2,.)			// Include SD
		
	* Fill matrix - For all blocks
		loc j = 1	
		foreach x of varlist $outcomes {
			
			if "`x'" != "mnz_day_correct_avg" loc r "r(mean)"
			if "`x'" == "mnz_day_correct_avg" loc r "r(mean)*100"
			* Stats			
				sum `x' if !missing(mnz_correct_svy)
				mat de[`j',1] = `r'
				mat de[`j',2] = r(sd)
				
					loc ++j
		}
	
		mat rownames de = $outcomes

	* Row labels
		la var mnz_visit_count 			"Avg. # of patrols per block"
		la var mnz_duration_patrol_avg 	"Avg. length of patrol"
		la var n_patrollers_avg 		"Avg. # of soldiers per patrol"
		la var mnz_day_correct_avg		"Avg. \% of patrols on correct block per night"
	
		frmttable, tex statmat(de) substat(1)		///
			replace fr sdec(2) ctitle("","All blocks") varlabel basefont(normalsize rm) statfont(rm)

		
//----//
//- Descriptive data on patrols  
//----//
	
//- Second part: % of patrols with at least 1
	
	use "$data/raw/patrols_data.dta", clear

	* Set vars
		global outcomes1 mnz_frisk_count mnz_idcheck_count mnz_drug_count mnz_arrest_count mnz_reten_count
	
	* Define matrix
		loc r = `:word count $outcomes1'
		mat de = J(`r',1,.)
				
		count if mnz_correct_svy == 0 | mnz_correct_svy == 1
		loc tot_mnz_all = r(N)
	
	* Fill matrix - For all blocks
		loc j = 1	
		foreach x of varlist $outcomes1 {
			
			* Part 1: % of patrols with at least 1 _________					
				count if `x' >= 1 & !missing(mnz_correct_svy)
				mat de[`j',1] = (r(N) / `tot_mnz_all')*100
						
					loc ++j
		}
		
		mat rownames de = $outcomes1
		
	* Row labels
		la var mnz_frisk_count 			"\% of patrols with at least 1 frisk"
		la var mnz_idcheck_count 		"\% of patrols with at least 1 ID check"
		la var mnz_drug_count 			"\% of patrols with at least 1 drug seizure"
		la var mnz_arrest_count			"\% of patrols with at least 1 arrest"
		la var mnz_reten_count			"\% of patrols with at least 1 detention"	
		
	* Export 
		frmttable using "$tables/table_A2", tex statmat(de) 		///
				append fr sdec(3) ctitle("", "All blocks") varlabel 			///
				basefont(normalsize rm) statfont(rm)
	
	
						
				//--------------------------------------------------//
				//-- Table A.3: Balance using administrative data --//
				//--------------------------------------------------//

//-- Loan administrative data prior to the intervention 

	use "$data/raw/admin_data_prior.dta", clear


//-- Define globals to run the loops

	global c_geo number_buildings_sampling area bat_min cai_min ptr_min
	global outcomes_s prior_homicides_num prior_robberies_num prior_drugdeal_num prior_wbearing_num
	global index prior_unw_crime2_num
	global all $index $outcomes_s
	

//-- Create the outline of the table 	

	gen panel_a = .
	gen panel_b = .
	gen panel_c = .
	gen panel_d = .
	
	la var panel_a "\textbf{\textit{Panel A: Index without controls}}"
	la var panel_b "\textbf{\textit{Panel B: Components without controls}}"
	la var panel_c "\textbf{\textit{Panel C: Index with controls}}"
	la var panel_d "\textbf{\textit{Panel D: Components with controls}}"
		
	mat coefs = J(`:word count $all'*2+`:word count $c_geo'*2+4,6,.) // Four panels. + Geographic controls. That's the reason of the 6:
												// 3 for the index estimation, and 3 for the components estimation.
												
	mat pvalues = J(rowsof(coefs),colsof(coefs),.)
	mat confint = J(rowsof(coefs),colsof(coefs),.)
	mat f = J(7,colsof(coefs)-3,.)	// 6 rows because Rob asked for 6 f tests + 1 for observations
	mat fpval = f

	
//-- Calculate statistics for each variable 	

	loc j = 1
	loc jf = 1
	foreach g of varlist control treat spillover {
	
	*Without controls
	
		qui reg `g' prior_unw_crime2_num i.barrio_code [pweight = iweight]
			mat coefs[2,`j'] = _b[prior_unw_crime2_num]
			mat coefs[2,`j'+1] = _se[prior_unw_crime2_num]
			mat pvalues[2,`j'] = round(2*ttail(e(df_r), abs(_b[prior_unw_crime2_num]/_se[prior_unw_crime2_num])),0.001)
			mat confint[2, `j'] = r(table)["ll", "prior_unw_crime2_num"]
			mat confint[2, `j'+1] = r(table)["ul", "prior_unw_crime2_num"]
			
			test prior_unw_crime2_num
			mat f[2,`jf'] = r(F)
			mat fpval[2,`jf'] = r(p)
			
		qui reg `g' $outcomes_s i.barrio_code [pweight = iweight]
			
			loc r = 4
			foreach l in $outcomes_s {
				mat coefs[`r',`j'] = _b[`l']
				mat coefs[`r',`j'+1] = _se[`l']
				mat pvalues[`r',`j'] = round(2*ttail(e(df_r), abs(_b[`l']/_se[`l'])),0.001)	
				mat confint[`r',`j'] = r(table)["ll", "`l'"]
				mat confint[`r',`j'+1] = r(table)["ul", "`l'"]
					loc ++r
			}
			
			test $outcomes_s
			mat f[3,`jf'] = r(F)
			mat fpval[3,`jf'] = r(p)					

	* With controls 
		qui reg `g' prior_unw_crime2_num i.barrio_code $c_geo [pweight = iweight]
			
			loc r1 = `r'+1
			foreach l1 in prior_unw_crime2_num $c_geo {
				mat coefs[`r1',`j'] = _b[`l1']
				mat coefs[`r1',`j'+1] = _se[`l1']
				mat pvalues[`r1',`j'] = round(2*ttail(e(df_r), abs(_b[`l1']/_se[`l1'])),0.001)		
				mat confint[`r1',`j'] = r(table)["ll", "`l1'"]
				mat confint[`r1',`j'+1] = r(table)["ul", "`l1'"]
					loc ++r1
			}
			
			test prior_unw_crime2_num
			mat f[4,`jf'] = r(F)
			mat fpval[4,`jf'] = r(p)

			// index and geo controls together
			test prior_unw_crime2_num $c_geo
			mat f[5,`jf'] = r(F)
			mat fpval[5,`jf'] = r(p)
		
		qui reg `g' $outcomes_s i.barrio_code $c_geo [pweight = iweight]
			loc r2 = `r1'+1
			foreach l2 in $outcomes_s $c_geo {
				mat coefs[`r2',`j'] = _b[`l2']
				mat coefs[`r2',`j'+1] = _se[`l2']
				mat pvalues[`r2',`j'] = round(2*ttail(e(df_r), abs(_b[`l2']/_se[`l2'])),0.001)	
				mat confint[`r2',`j'] = r(table)["ll", "`l2'"]
				mat confint[`r2',`j'+1] = r(table)["ul", "`l2'"]
					loc ++r2
			}
		
			test $outcomes_s
			mat f[6,`jf'] = r(F)
			mat fpval[6,`jf'] = r(p)
			
			// Components and geo controls together
			test $outcomes_s $c_geo
			mat f[7,`jf'] = r(F)
			mat fpval[7,`jf'] = r(p)
			
		loc j = `j'+2
		loc ++jf

	}
	  
	  
	* Fill matrix of F-statistics with number of observations 
	
	count
		mat f[1,1] = r(N)
		mat f[1,2] = r(N)
		mat f[1,3] = r(N)
		
	
	* Create matrix with coefs and p-values instead of standard deviations
	
	mat colnames coefs = Control SE_cont Treatment SE_treat Spillover SE_spill
	
	mat coef_pval = J(rowsof(coefs),12,.)
	
	foreach n of numlist 1/24{
	     
		 * Fill columns with coefs
	    matrix coef_pval[`n', 1] = coefs[`n', 1]
		matrix coef_pval[`n', 5] = coefs[`n', 3]
		matrix coef_pval[`n', 9] = coefs[`n', 5]
		
		* Fill lower bound of confidence interval
	    matrix coef_pval[`n', 2] = confint[`n', 1]
		matrix coef_pval[`n', 6] = confint[`n', 3]
		matrix coef_pval[`n', 10] = confint[`n', 5]
		
		* Fill upper bound of confidence interval
	    matrix coef_pval[`n', 3] = confint[`n', 2]
		matrix coef_pval[`n', 7] = confint[`n', 4]
		matrix coef_pval[`n', 11] = confint[`n', 6]
		
		* Fill columns with pvals
	    matrix coef_pval[`n', 4] = pvalues[`n', 1]
		matrix coef_pval[`n', 8] = pvalues[`n', 3]
		matrix coef_pval[`n', 12] = pvalues[`n', 5]
		
	}
	
	mat colnames coef_pval = Control conf_l conf_u pval Treatment conf_l conf_u pval Spillover conf_l conf_u pval
	mat rownames coef_pval = panel_a $index panel_b $outcomes_s panel_c $index $c_geo panel_d $outcomes_s $c_geo

	* Matrix to organize the table
	matrix dcols = (0,0,1,0,0,0,1,0,0,0,1,0)
	
	* Labels for output
	la var prior_unw_crime2_num "Crime index"
	la var prior_homicides_num "Homicides"
	la var prior_robberies_num "Robberies"
	la var prior_drugdeal_num "Drug dealing"
	la var prior_wbearing_num "llegal possession of a firearm"
	la var number_buildings_s "Number of buildings on block"
	la var area "Area of block"
	la var bat_min "Distance to nearest army battalion (meters)"
	la var cai_min "Distance to nearest police station (meters)"
	la var ptr_min "Distance to nearest public transportation hub (meters)"
	
	
	* Export table 	
	frmttable, tex statmat(coef_pval) ///
		replace fr varlabels sdec(3) substat(2) doubles(dcols) squarebrack basefont(normalsize rm) statfont(rm)

	
	* Create matrix with F-stats and pvalues 
	mat f_pval = J(rowsof(f),6,.)
	
	foreach n of numlist 1/7{
	     
		 * Fill columns with coefs
	    matrix f_pval[`n', 1] = f[`n', 1]
		matrix f_pval[`n', 3] = f[`n', 2]
		matrix f_pval[`n', 5] = f[`n', 3]
		
		* Fill columns with pvals
	    matrix f_pval[`n', 2] = fpval[`n', 1]
		matrix f_pval[`n', 4] = fpval[`n', 2]
		matrix f_pval[`n', 6] = fpval[`n', 3]
		
	}
	
	
	* Labels for output
	
	mat rownames f_pval = obs panel_a panel_b panel_c1 panel_c2 panel_d1 panel_d2
	
	gen obs = .
	gen panel_c1 = .
	gen panel_c2 = .
	gen panel_d1 = .
	gen panel_d2 = .
	
	la var obs "Observations"
	la var panel_a "\textbf{Panel A}: F-stat on index without controls"
	la var panel_b "\textbf{Panel B}: F-stat on components without controls"
	la var panel_c1 "\textbf{Panel C}: F-stat on index"
	la var panel_c2 "\textbf{Panel C}: F-stat on index with controls"
	la var panel_d1 "\textbf{Panel D}: F-stat on components"
	la var panel_d2 "\textbf{Panel D}: F-stat on components with controls"
	
	
	* Append f stats and observations
	
	frmttable using "$tables/table_A3.tex", tex statmat(f_pval)  									///
		fr substat(1) varlabels 	///
		sdec(0 \ 3 \ 3 \ 3 \ 3 \ 3 \ 3) append basefont(normalsize rm) statfont(rm)
		
				
				
			//----------------------------------------------------//
			//-- Table A.4. Balance using endline survey data   --//
			//----------------------------------------------------//
							
//----//
//- Balance of endline data 
//----//

//- generating balance table
	
	use "$data/raw/survey_endline.dta", clear
	
	* For DE
		ta treatment, g(treatment_d)

	* Vars to be tested
		#delim ;
			global demovars age gender educ ;
			global geovars number_buildings_sampling area bat_min cai_min ptr_min ;
			global cvars $demovars $geovars ;
		#delim cr
	
		gen panel_a = .
		gen panel_b = .
			la var panel_a "\textbf{\textit{Panel A: Demographic controls}}"
			la var panel_b "\textbf{\textit{Panel B: Demographic and geo controls}}"

		loc d = `: word count $demovars'
		loc p = `: word count $geovars'
		loc r = `d'+`p'
		
		mat DE = J(`d'*2+`p'+2,6,.)	   				// Demo only, demo + geo 
		mat confint = J(rowsof(DE),colsof(DE),.)
		mat pvalues = J(rowsof(DE),colsof(DE),.)
		mat f = J(4,colsof(DE)-3,.)	// 4 rows because Rob asked for 3 f tests + 1 for observations
		mat fpval = f
		
		loc j = 1
		loc jf = 1
		foreach var of varlist treatment_d* {
			
		* Demo controls 
			qui reg `var' $demovars [pweight = iweight], vce(cluster manzana_code)  
				
				loc n = 2
				foreach l in $demovars {
					mat DE[`n',`j'] = _b[`l']
					mat DE[`n',`j'+1] = _se[`l']
					mat pvalues[`n',`j'] = round(2*ttail(e(df_r), abs(_b[`l']/_se[`l'])),0.001)	
					mat confint[`n',`j'] = r(table)["ll", "`l'"]
					mat confint[`n',`j'+1] = r(table)["ul", "`l'"]
						loc ++n
				}

				test $demovars
				mat f[2,`jf'] = r(F)
				mat fpval[2,`jf'] = r(p)					

		* Demo + geo controls 
			qui reg `var' $demovars $geovars [pweight = iweight], vce(cluster manzana_code)  
			
				loc n1 = `n'+1
				foreach l1 in $demovars $geovars {
					mat DE[`n1',`j'] = _b[`l1']
					mat DE[`n1',`j'+1] = _se[`l1']
					mat pvalues[`n1',`j'] = round(2*ttail(e(df_r), abs(_b[`l1']/_se[`l1'])),0.001)
					mat confint[`n1',`j'] = r(table)["ll", "`l1'"]
					mat confint[`n1',`j'+1] = r(table)["ul", "`l1'"]
						loc ++n1
				}
				
				// Only demovars
				test $demovars
				mat f[3,`jf'] = r(F)
				mat fpval[3,`jf'] = r(p)					
			
				// Demo + geovars
				test $demovars $geovars
				mat f[4,`jf'] = r(F)
				mat fpval[4,`jf'] = r(p)
				
			loc j = `j'+2 
			loc ++jf
			
				
		}
		
	* Fill matrix of F-stats with number of observations 	
		count 
			mat f[1,1] = r(N)
			mat f[1,2] = r(N)
			mat f[1,3] = r(N)
	
	
	* Create matrix with coefs and p-values instead of standard deviations
	
	mat colnames DE = Control SE_cont Treatment SE_treat Spillover SE_spill
	
	mat coef_pval = J(rowsof(DE),12,.)
	
	foreach n of numlist 1/13{
	     
		 * Fill columns with coefs
	    matrix coef_pval[`n', 1] = DE[`n', 1]
		matrix coef_pval[`n', 5] = DE[`n', 3]
		matrix coef_pval[`n', 9] = DE[`n', 5]
		
		* Fill lower bound of confidence interval
	    matrix coef_pval[`n', 2] = confint[`n', 1]
		matrix coef_pval[`n', 6] = confint[`n', 3]
		matrix coef_pval[`n', 10] = confint[`n', 5]
		
		* Fill upper bound of confidence interval
	    matrix coef_pval[`n', 3] = confint[`n', 2]
		matrix coef_pval[`n', 7] = confint[`n', 4]
		matrix coef_pval[`n', 11] = confint[`n', 6]
		
		* Fill columns with pvals
	    matrix coef_pval[`n', 4] = pvalues[`n', 1]
		matrix coef_pval[`n', 8] = pvalues[`n', 3]
		matrix coef_pval[`n', 12] = pvalues[`n', 5]
		
	}
	
		mat colnames coef_pval = Control conf_l conf_u pval Treatment conf_l conf_u pval Spillover conf_l conf_u pval
		mat rownames coef_pval = panel_a $demovars panel_b $demovars $geovars

	* Matrix to organize the table
		matrix dcols = (0,0,1,0,0,0,1,0,0,0,1,0)
	
	* Labels
		la var age "Age"
		la var gender "Gender"
		la var educ "Education (years)"
		la var number_buildings_sam "Number of buildings on the block"
		la var area "Area of block"
		la var bat_min "Distance to nearest army battalion (meters)" 
		la var cai_min "Distance to nearest police station (meters)" 
		la var ptr_min "Distance to nearest public transportation hub (meters)"

		
	* Export table 	
		frmttable, tex statmat(coef_pval) 			 ///
				replace fr varlabels sdec(3) substat(2) doubles(dcols) squarebrack ///
				basefont(normalsize rm) statfont(rm)
				
	
	* Create matrix with F-stats and pvalues 
	mat f_pval = J(rowsof(f),6,.)
	
	foreach n of numlist 1/4{
	     
		 * Fill columns with coefs
	    matrix f_pval[`n', 1] = f[`n', 1]
		matrix f_pval[`n', 3] = f[`n', 2]
		matrix f_pval[`n', 5] = f[`n', 3]
		
		* Fill columns with pvals
	    matrix f_pval[`n', 2] = fpval[`n', 1]
		matrix f_pval[`n', 4] = fpval[`n', 2]
		matrix f_pval[`n', 6] = fpval[`n', 3]
		
	}
	
	
	* Labels for output
	
	mat rownames f_pval = obs panel_a panel_b1 panel_b2
	
	gen obs = .
	gen panel_b1 = .
	gen panel_b2 = .
	
	la var obs "Observations"
	la var panel_a "\textbf{Panel A}: F-stat individual-level controls"
	la var panel_b1 "\textbf{Panel B}: F-stat on block-level controls"
	la var panel_b2 "\textbf{Panel B}: F-stat on individual and block-level controls"
	
	
	* Append f stats and observations
		frmttable using "$tables/table_A4.tex", tex statmat(f_pval)  			///
			fr  substat(1) varlabels	///
			sdec(0 \ 3 \ 3 \ 3) append basefont(normalsize rm) statfont(rm)									


				
			//-------------------------------------------------------------//
			//-- Table A.5: Treatment effects on crime using alternative --//
			//			approaches to estimating spillover effects   	   //
			//-------------------------------------------------------------//

//-------//					
//- Regressions with data admin data (during the intervention)
//-------//	

	use "$data/raw/admin_data_during.dta", clear
	
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	global blockdemovars block_age block_educ block_pct_male

	
	//- Linear decay 
	
	gen treatvar = id_treat
	gen spillvar = sum_distance_t_lin_std
	
	reg unw_crime2_num treatvar spillvar /// 
		i.barrio_code $geovars $blockdemovars cum_all_unw_crime2_num [pweight=iweight]
	
	estadd matrix table = r(table)
	estimates store lin_dur
	
	drop treatvar spillvar 
	
	
	//- Exponential decay 
	
	gen treatvar = id_treat
	gen spillvar = sum_distance_t_exp_std
	
	reg unw_crime2_num treatvar spillvar /// 
		i.barrio_code $geovars $blockdemovars cum_all_unw_crime2_num [pweight=iweight]
	
	estadd matrix table = r(table)
	estimates store exp_dur
	
	drop treatvar spillvar
	
	
	// Share of treated blocks 
	
	gen treatvar = prop_treatment
	
	reg unw_crime2_num treatvar  /// 
		i.barrio_code $geovars $blockdemovars cum_all_unw_crime2_num [pweight=iweight] ///
		if treatment == 1
	
	estadd matrix table = r(table)
	estimates store ptr_dur
	
	
//-------//					
//- Regressions with data admin data (after the intervention)
//-------//		
		
	use "$data/raw/admin_data_after.dta", clear
	
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	global blockdemovars block_age block_educ block_pct_male

	
	//- Linear decay
	
	gen treatvar = id_treat
	gen spillvar = sum_distance_t_lin_std
	
	reg unw_crime_num  treatvar spillvar /// 
		i.barrio_code $geovars $blockdemovars cum_all_unw_crime_num [pweight=iweight]
	
	estadd matrix table = r(table)
	estimates store lin_aft

	drop treatvar spillvar
	
	
	//- Exponential decay 
	
	gen treatvar = id_treat
	gen spillvar = sum_distance_t_exp_std
	
	reg unw_crime_num  treatvar spillvar /// 
		i.barrio_code $geovars $blockdemovars cum_all_unw_crime_num [pweight=iweight]
	
	estadd matrix table = r(table)
	estimates store exp_aft
	
	drop treatvar spillvar
	
	
	// Share of treated blocks 
	
	gen treatvar = prop_treatment
	
	reg unw_crime_num treatvar  /// 
		i.barrio_code $geovars $blockdemovars cum_all_unw_crime_num [pweight=iweight] ///
		if treatment == 1
	
	estadd matrix table = r(table)
	estimates store ptr_aft 
	
	
//-------//					
//- Export regressions
//-------//	
	
	loc var1 `"\shortstack{During \\ intervention}"'
	loc var2 `"\shortstack{After \\ intervention}"'
	loc var3 `"\shortstack{During \\ intervention}"'
	loc var4 `"\shortstack{After \\ intervention}"'
	loc var5 `"\shortstack{During \\ intervention}"'
	loc var6 `"\shortstack{After \\ intervention}"'
	
	#delimit ;
		loc optex   "nonumbers nonotes nodepvar nomtitles label noeqlines nogaps
					  star(* 0.10 ** 0.05 *** 0.01) f booktabs nonotes compress
					  b(%9.3fc) se(%9.2fc)" ;

		esttab lin_dur lin_aft exp_dur exp_aft ptr_dur ptr_aft 
			using "$tables/table_A5.tex", 
			keep(treatvar spillvar) 
			legend booktabs f replace b(%9.3fc) se(%9.2fc)
			order(mytreat myspill)
			indicate("Individual controls = ${dem}" "Neighborhood FE = *.barrio_code*" "Block-level controls = cum_all_*", labels("Yes" "No")) ///
			stats(N r2, fmt(0 2 3) labels("Observations" "\$R^2$")) ///
			nostar	///
			cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
			varlabels(treatvar "Treatment" spillvar "Spillover") nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var3'"' `"`var4'"' `"`var5'"' `"`var6'"')  ///
			collabels(none) ///
			nolines														///
			prehead( 													///
			   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
			   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
			   \noalign{\smallskip} 									///
			   &  \multicolumn{6}{c}{\textbf{Crime index}} \\	    	///
			   \cline{2-7} 				 								///
			   & \multicolumn{2}{c}{\textbf{Linear Decay}} 
			   & \multicolumn{2}{c}{\textbf{Exponential Decay}} 
			   & \multicolumn{2}{c}{\textbf{Prop. Treated Blocks}}  \\			///
				  \cline{2-3} \cline{4-5} \cline{6-7} 						///		   
		   \noalign{\smallskip}									///
		   )  															///
		   posthead(\hline) 															///
		   postfoot(  													///
			  \noalign{\smallskip} \hline 					///
			  \end{tabular} 											///
			  \medskip     												///
		   )
			;	
	#delimit cr	
				

				
			//--------------------------------------------------------//
			//-- 	Table A.6. Treatment effects on crime, crime    --//
			//		 victimization, and witnessing crime using 	      //
			// 		lenient correction for multiple comparisons		  //
			//--------------------------------------------------------//

//-------//
//-- Run regressions and store p-values 	 
//-------//	
			
//-- Define matrix for multiple hypothesis comparison. P-values. --//

	mat table1t = J(5,1,.)
	mat table1s = J(5,1,.)

		
//-- Column 1 --//
	
	//- Load data admin data (during the intervention)
	
	use "$data/raw/admin_data_during.dta", clear
	
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	global blockdemovars block_age block_educ block_pct_male

	
	//- Run regression with crime during intervention 
	
	reg unw_crime2_num  i.treatment /// 
		i.barrio_code $geovars $blockdemovars cum_all_unw_crime2_num [pweight=iweight]
	
	qui sum unw_crime2_num if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t1_1
	
	//- Store p-values
	
	mat table1t[1,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table1s[1,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))		
	
	
//-- Column 2 --//

	//- Load admin data (after the intervention)
	
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	global blockdemovars block_age block_educ block_pct_male
				
	use "$data/raw/admin_data_after.dta", clear
	

	//- Run regression with crime after intervention 

	reg unw_crime_num  i.treatment /// 
		i.barrio_code $geovars $blockdemovars cum_all_unw_crime_num [pweight=iweight]
	
	qui sum unw_crime_num if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t1_2
	
	//- Store p-values
	
	mat table1t[2,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table1s[2,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))


	
//-- Columns 3, 4 y 5--//

	//- Load data of the endline survey

	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 

	
	//- Crime victimization during intervention

	reg i2_victimduringindex_std  i.treatment /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i2_victimduringindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t1_3

	//- Store p-values
	mat table1t[3,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table1s[3,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))

					 
	//- Crime victimization  after intervention
	
	reg i2_victimafterindex_std  i.treatment /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i2_victimafterindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t1_4
	
	//- Store p-values
	mat table1t[4,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table1s[4,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))

	

	//- Crime witnessing after intervention 

	reg i_witnessindex_std  i.treatment /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_witnessindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t1_5
	
	//- Store p-values
	mat table1t[5,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table1s[5,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))
	
	
//-------//
//-- Apply correction and Export 		 
//-------//

//-- Apply corrections 
	
	global t1vars w_crime_num_std_d w_crime_num_std_a i_victimdur i_victimpostindex_std i_witnessind

	mat rownames table1t = $t1vars
	mat rownames table1s = $t1vars

	* Add corrected p-values

	* - BH q value (1995)
		bhcorr, mat(table1t) newname(pvalue) outmat(table1ta)
		bhcorr, mat(table1s) newname(pvalue) outmat(table1sa)

	* - BH q threshold
		bhcorralt, mat(table1t) newname(pvalue) fdr(0.1) outmat(table1tb)
		bhcorralt, mat(table1s) newname(pvalue) fdr(0.1) outmat(table1sb)

	* - Holms (1979)
		holmscorr, mat(table1t) newname(pvalue) fwer(0.1) outmat(table1tc)
		holmscorr, mat(table1s) newname(pvalue) fwer(0.1) outmat(table1sc)

							   
	//-- Add checkmark to the latex table 
	
	checkmark_yes, outcomes(${t1vars}) mat1t(table1ta) mat1s(table1sa) ///
								   mat2t(table1tb) mat2s(table1sb) ///
								   mat3t(table1tc) mat3s(table1sc) ename(t1_)
								   
								   
	//-- Export the regressions 
	
	loc var1 `"\shortstack{During \\ intervention}"'
	loc var2 `"\shortstack{After \\ intervention}"'
	loc var3 `"\shortstack{During \\ intervention}"'
	loc var4 `"\shortstack{After \\ intervention}"'
	loc var5 `"\shortstack{After \\ intervention}"'
	
	esttab t1_1 t1_2 t1_3 t1_4 t1_5 ///
		using "$tables\table_A6.tex", ///
		legend booktabs f replace b(%9.3fc) se(%9.2fc) ///
		order(1.treatment 2.treatment) compress ///
		indicate("Individual controls = ${demovars}" "Neighborhood FE = *.barrio_code" "Block-level controls = cum_all_* ${geovars}", labels("Yes" "No")) ///
		keep(1.treatment 2.treatment) ///
		stats(N r2 c_mean qval qval2 text text1 text2 text3, fmt(0 2 3 3 3) labels("Observations" "\$R^2$" "Control mean" "qval-treatment" "qval-spillover" "BH-treatment" "BH-spillover" "Holm-treatment" "Holm-spillover")) ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var3'"' `"`var4'"' `"`var5'"')  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   &  \multicolumn{2}{c}{\textbf{Admin data}} & 			///
			  \multicolumn{3}{c}{\textbf{Endline survey}} \\	    	///
		   \cline{2-3} \cline{4-6} 								///
		   &  \multicolumn{2}{c}{\textbf{Crime incidence}} &		///
			  \multicolumn{2}{c}{\textbf{Crime victimization}} &	///
			  \multicolumn{1}{c}{\textbf{Crime witnessing}} \\			///
			  \cline{2-3} \cline{4-5} \cline{6-6} 						///		   
	   \noalign{\smallskip}									///
	   )  															///
	   posthead(\hline) 															///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 					///
		  \end{tabular} 											///
		  \medskip     												///
	   )
	   
	   		
			//--------------------------------------------------------//
			//-- 	Table A.7. Treatment effects on crime, crime    --//
			//		 victimization, and witnessing crime using 	      //
			// 		stringent correction for multiple comparisons		  //
			//--------------------------------------------------------//

//-------//
//-- Run regressions and store p-values 	 
//-------//	
			
//-- Define matrix for multiple hypothesis comparison. P-values. --//

	mat table1t = J(5,1,.)
	mat table1s = J(5,1,.)

		
//-- Column 1 --//
	
	//- Load data admin data (during the intervention)
	
	use "$data/raw/admin_data_during.dta", clear
	
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	global blockdemovars block_age block_educ block_pct_male

	
	//- Run regression with crime during intervention 
	
	reg unw_crime2_num  i.treatment /// 
		i.barrio_code $geovars $blockdemovars cum_all_unw_crime2_num [pweight=iweight]
	
	qui sum unw_crime2_num if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t1_1
	
	//- Store p-values
	
	mat table1t[1,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table1s[1,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))		
	
	
//-- Column 2 --//

	//- Load admin data (after the intervention)
	
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	global blockdemovars block_age block_educ block_pct_male
				
	use "$data/raw/admin_data_after.dta", clear
	

	//- Run regression with crime after intervention 

	reg unw_crime_num  i.treatment /// 
		i.barrio_code $geovars $blockdemovars cum_all_unw_crime_num [pweight=iweight]
	
	qui sum unw_crime_num if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t1_2
	
	//- Store p-values
	
	mat table1t[2,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table1s[2,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))


	
//-- Columns 3, 4 y 5--//

	//- Load data of the endline survey

	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 

	
	//- Crime victimization during intervention

	reg i2_victimduringindex_std  i.treatment /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i2_victimduringindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t1_3

	//- Store p-values
	mat table1t[3,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table1s[3,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))

					 
	//- Crime victimization  after intervention
	
	reg i2_victimafterindex_std  i.treatment /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i2_victimafterindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t1_4
	
	//- Store p-values
	mat table1t[4,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table1s[4,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))

	

	//- Crime witnessing after intervention 

	reg i_witnessindex_std  i.treatment /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_witnessindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t1_5
	
	//- Store p-values
	mat table1t[5,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table1s[5,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))
	
	
//-------//
//-- Apply correction and Export 	 
//-------//

//-- Apply corrections 
	
	global t1vars w_crime_num_std_d w_crime_num_std_a i_victimdur i_victimpostindex_std i_witnessind

	mat rownames table1t = $t1vars
	mat rownames table1s = $t1vars

	* More stringent correction
		/*
			Each indep. variable is a different hypothesis. Now I run the corrections
			using treatment+spillover as my number to be corrected.
			
			In order to identify if the coefficient belongs to treatment or spillover,
			I include an additional column: 1 for treatment, 2 for spillover.
		*/

	mat it1 = J(rowsof(table1t), colsof(table1t), 1)
	mat is1 = J(rowsof(table1s), colsof(table1s), 2)
		
	mat table1all = (table1t, it1) \ (table1s, is1)
			
	* Separate matrices. I have to do this in order to append checkmarks to estimated results.
	* I define the following locals and then I separate each matrix after each correction
			loc nt = rowsof(table1all)/2
			loc ns = `nt'+1
			
	* Add corrected p-values

		* - BH q value (1995)
			bhcorr, mat(table1all) newname(pvalue) outmat(table1alla)
				
				mat table1talla = table1alla[1..`nt',1...]
				mat table1salla = table1alla[`ns'...,1...]
		
		* - BH q threshold
			bhcorralt, mat(table1all) newname(pvalue) fdr(0.1) outmat(table1allb)
								
				mat table1tallb = table1allb[1..`nt',1...]
				mat table1sallb = table1allb[`ns'...,1...]
			
		* - Holms (1979)
			holmscorr, mat(table1all) newname(pvalue) fwer(0.1) outmat(table1allc)
								
				mat table1tallc = table1allc[1..`nt',1...]
				mat table1sallc = table1allc[`ns'...,1...]
				   

	//-- Add checkmark to the latex table 
	
	checkmark_yes, outcomes(${t1vars}) mat1t(table1talla) mat1s(table1salla) ///
									   mat2t(table1tallb) mat2s(table1sallb) ///
									   mat3t(table1tallc) mat3s(table1sallc) ename(t1_)

									   
	//-- Export the regressions 
									   
	loc var1 `"\shortstack{During \\ intervention}"'
	loc var2 `"\shortstack{After \\ intervention}"'
	loc var3 `"\shortstack{During \\ intervention}"'
	loc var4 `"\shortstack{After \\ intervention}"'
	loc var5 `"\shortstack{After \\ intervention}"'
	
	esttab t1_1 t1_2 t1_3 t1_4 t1_5 ///
		using "$tables\table_A7.tex", ///
		legend booktabs f replace b(%9.3fc) se(%9.2fc) ///
		order(1.treatment 2.treatment) compress ///
		indicate("Individual controls = ${demovars}" "Neighborhood FE = *.barrio_code" "Block-level controls = cum_all_* ${geovars}", labels("Yes" "No")) ///
		keep(1.treatment 2.treatment) ///
		stats(N r2 c_mean qval qval2 text text1 text2 text3, fmt(0 2 3 3 3) labels("Observations" "\$R^2$" "Control mean" "qval-treatment" "qval-spillover" "BH-treatment" "BH-spillover" "Holm-treatment" "Holm-spillover")) ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var3'"' `"`var4'"' `"`var5'"')  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   &  \multicolumn{2}{c}{\textbf{Admin data}} & 			///
			  \multicolumn{3}{c}{\textbf{Endline survey}} \\	    	///
		   \cline{2-3} \cline{4-6} 								///
		   &  \multicolumn{2}{c}{\textbf{Crime incidence}} &		///
			  \multicolumn{2}{c}{\textbf{Crime victimization}} &	///
			  \multicolumn{1}{c}{\textbf{Crime witnessing}} \\			///
			  \cline{2-3} \cline{4-5} \cline{6-6} 						///		   
	   \noalign{\smallskip}									///
	   )  															///
	   posthead(\hline) 															///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 					///
		  \end{tabular} 											///
		  \medskip     												///
	   )
	   			
			
			//-----------------------------------------------------//
			//-- 	Table A.8. Treatment effects on perceptions  --//
			//	      of security using lenient correction for     //
			//				multiple comparisons 				   //
			//-----------------------------------------------------//

//-------//
//-- Run regressions and store p-values 	 
//-------//			
			
//- Define matrix for multiple hypothesis comparison. P-values.

	mat table2t = J(3,1,.)
	mat table2s = J(3,1,.)
	
	
//- Load data (endline)
					
	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
		

//-- Column 1 (all security)

	reg i_securityallindex_std  i.treatment i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
		vce(cluster manzana_code) baselevels
	
	qui sum i_securityallindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t2_1,  title("Police")
	
	//- Store p-values
	
	mat table2t[1,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table2s[1,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))
	
		
//-- Column 2 (security residents)

	reg i_securityallindex_ps  i.treatment i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
		vce(cluster manzana_code) baselevels
		
	qui sum i_securityallindex_ps if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t2_2,  title("Police")
	
	// -- Store p-values
	
	mat table2t[2,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table2s[2,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))
	
	
//-- Column 3 (business owners)

	reg i_business  i.treatment i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
		vce(cluster manzana_code) baselevels
		
	qui sum i_business if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t2_3,  title("Police")
			
	// -- Store p-values
	
	mat table2t[3,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table2s[3,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))
			

//-------//
//-- Apply correction and Export 	 
//-------//

	//-- Apply the corrections 
	
	global t2vars i_securityallindex_std i_securityallindex_ps i_business
	
	mat rownames table2t = $t2vars
	mat rownames table2s = $t2vars

* Add corrected p-values

	* - BH q value (1995)
		bhcorr, mat(table2t) newname(pvalue) outmat(table2ta)
		bhcorr, mat(table2s) newname(pvalue) outmat(table2sa)
	
	* - BH q threshold
		bhcorralt, mat(table2t) newname(pvalue) fdr(0.1) outmat(table2tb)
		bhcorralt, mat(table2s) newname(pvalue) fdr(0.1) outmat(table2sb)
	
	* - Holms (1979)
		holmscorr, mat(table2t) newname(pvalue) fwer(0.1) outmat(table2tc)
		holmscorr, mat(table2s) newname(pvalue) fwer(0.1) outmat(table2sc)

		
	//-- Add checkmark to the latex table 
	
	checkmark_yes, outcomes(${t2vars}) mat1t(table2ta) mat1s(table2sa) ///
									   mat2t(table2tb) mat2s(table2sb) ///
									   mat3t(table2tc) mat3s(table2sc) ename(t2_)
	
	//-- Export the regressions 
	
	loc var1 `"\textbf{\shortstack{All \\ safety}}"'
	loc var2 `"\textbf{\shortstack{Personal \\ safety}}"'
	loc var3 `"\textbf{\shortstack{Business \\ safety}}"'
	
	esttab t2_1 t2_2 t2_3 ///
		using "$tables\table_A8.tex", ///
		legend booktabs f replace b(%9.3fc) se(%9.2fc) ///
		order(1.treatment 2.treatment) compress ///
		indicate("Individual-level controls = ${demovars}" "Block-level controls = ${geovars}" "Neighborhood FE = *.barrio_code", labels("Yes" "No")) ///
		keep(1.treatment 2.treatment) ///
		stats(N r2 c_mean qval qval2 text text1 text2 text3, fmt(0 2 3 3 3) labels("Observations" "\$R^2$" "Control mean" "qval-treatment" "qval-spillover" "BH-treatment" "BH-spillover" "Holm-treatment" "Holm-spillover")) ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var3'"')  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   &  \multicolumn{3}{c}{\textbf{Endline survey}} \\	    	///
		   \cline{2-4} 	 											///
		   &  \multicolumn{3}{c}{\textbf{Perceptions of security}} \\			///
			  \cline{2-4} 											///		   
	   \noalign{\smallskip}											///
	   )  															///
	   posthead(\hline) 											///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 								///
		  \end{tabular} 											///
		  \medskip     												///
	   )
	   	
	
			//-----------------------------------------------------//
			//-- 	Table A.9. Treatment effects on perceptions  --//
			//	      of security using stringent correction for   //
			//				multiple comparisons 				   //
			//-----------------------------------------------------//

//-------//
//-- Run regressions and store p-values 	 
//-------//			
			
//- Define matrix for multiple hypothesis comparison. P-values.

	mat table2t = J(3,1,.)
	mat table2s = J(3,1,.)
	
	
//- Load data (endline)
					
	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
		

//-- Column 1 (all security)

	reg i_securityallindex_std  i.treatment i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
		vce(cluster manzana_code) baselevels
	
	qui sum i_securityallindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t2_1,  title("Police")
	
	//- Store p-values
	
	mat table2t[1,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table2s[1,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))
	
		
//-- Column 2 (security residents)

	reg i_securityallindex_ps  i.treatment i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
		vce(cluster manzana_code) baselevels
		
	qui sum i_securityallindex_ps if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t2_2,  title("Police")
	
	// -- Store p-values
	
	mat table2t[2,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table2s[2,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))
	
	
//-- Column 3 (business owners)

	reg i_business  i.treatment i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
		vce(cluster manzana_code) baselevels
		
	qui sum i_business if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t2_3,  title("Police")
			
	// -- Store p-values
	
	mat table2t[3,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table2s[3,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))
			

//-------//
//-- Apply correction and Export 	 
//-------//

//-- Apply the correction 

	global t2vars i_securityallindex_std i_securityallindex_ps i_business

	mat rownames table2t = $t2vars
	mat rownames table2s = $t2vars

	* More stringent correction	
	mat it1 = J(rowsof(table2t), colsof(table2t), 1)
	mat is1 = J(rowsof(table2s), colsof(table2s), 2)

	mat table2all = (table2t, it1) \ (table2s, is1)
		
	* Add corrected p-values
		
		loc mat "table2"
		
	* Separate matrices. I have to do this in order to append checkmarks to estimated results.
	* I define the following locals and then I separate each matrix after each correction
		loc nt = rowsof(`mat'all)/2
		loc ns = `nt'+1


	* - BH q value (1995)
		bhcorr, mat(`mat'all) newname(pvalue) outmat(`mat'alla)
			
			mat `mat'talla = `mat'alla[1..`nt',1...]
			mat `mat'salla = `mat'alla[`ns'...,1...]

	* - BH q threshold
		bhcorralt, mat(`mat'all) newname(pvalue) fdr(0.1) outmat(`mat'allb)
							
			mat `mat'tallb = `mat'allb[1..`nt',1...]
			mat `mat'sallb = `mat'allb[`ns'...,1...]
		
	* - Holms (1979)
		holmscorr, mat(`mat'all) newname(pvalue) fwer(0.1) outmat(`mat'allc)
							
			mat `mat'tallc = `mat'allc[1..`nt',1...]
			mat `mat'sallc = `mat'allc[`ns'...,1...]
		
		
	//-- Add checkmark to the latex table 
	
	checkmark_yes, outcomes(${t2vars}) mat1t(`mat'talla) mat1s(`mat'salla) ///
									   mat2t(`mat'tallb) mat2s(`mat'sallb) ///
									   mat3t(`mat'tallc) mat3s(`mat'sallc) ename(t2_)
	
	
	//-- Export the regressions 
	
	esttab t2_1 t2_2 t2_3 ///
		using  "$tables\table_A9.tex", /// 
		legend booktabs f replace b(%9.3fc) se(%9.2fc) ///
		order(1.treatment 2.treatment) compress ///
		indicate("Individual-level controls = ${demovars}" "Block-level controls = ${geovars}" "Neighborhood FE = *.barrio_code", labels("Yes" "No")) ///
		keep(1.treatment 2.treatment) ///
		stats(N r2 c_mean qval qval2 text text1 text2 text3, fmt(0 2 3 3 3) labels("Observations" "\$R^2$" "Control mean" "qval-treatment" "qval-spillover" "BH-treatment" "BH-spillover" "Holm-treatment" "Holm-spillover")) ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var3'"' `"`var4'"' `"`var5'"')  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   &  \multicolumn{3}{c}{\textbf{Endline survey}} \\	    	///
		   \cline{2-4} 	 											///
		   &  \multicolumn{3}{c}{\textbf{Perceptions of security}} \\			///
			  \cline{2-4} 											///		   
	   \noalign{\smallskip}											///
	   )  															///
	   posthead(\hline) 											///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 								///
		  \end{tabular} 											///
		  \medskip     												///
	   )
	
		
		
				//-------------------------------------------------//
				//-- 	Table A.10. Treatment effects on abuses  --//
				//	      using lenient correction for  		   //
				//				multiple comparisons 			   //
				//-------------------------------------------------//

//-------//
//-- Run regressions and store p-values 	 
//-------//

//-- Define matrix for multiple hypothesis comparison. P-values.
		mat table3t = J(4,1,.)
		mat table3s = J(4,1,.)

//----//								 
//- Regressions with monitoring data	
//----//
				
	use "$data/raw/survey_monitoring.dta", clear
	
	global indiv_control age gender 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	
	
//-- Column 1 (abuses by the police)	
					
	reg abuse_police  i.treatment i.barrio_code ${geovars} ${indiv_control} [pweight=iweight], ///
		vce(cluster manzana_code) baselevels
	
	qui sum abuse_police if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t3_1,  title("Police")
	
	//-- Store p-values
	
	mat table3t[1,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table3s[1,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))
	
	
//-- Column 2 (abuses by the military)

	reg abuse_military  i.treatment i.barrio_code ${geovars} ${indiv_control} [pweight=iweight], ///
		vce(cluster manzana_code) baselevels
	
	qui sum abuse_military if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t3_2,  title("Military")
	
	//-- Store p-values
	
	mat table3t[2,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table3s[2,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))
	


//----//								 
//- Regressions with endline data	
//----//

	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
		
	
//-- Column 3 (abuses by the police)

	reg abuse_police  i.treatment i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
		vce(cluster manzana_code) baselevels
		
	qui sum abuse_police if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t3_3,  title("Police")
	
	//-- Store p-values
	
	mat table3t[3,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table3s[3,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))
		
		
//-- Column 4 (military abuse)

	reg abuse_military  i.treatment i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
		
	qui sum abuse_military if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t3_4,  title("Military")
	
	//-- Store p-values
	
	mat table3t[4,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table3s[4,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))

	
//-- Rename vars for table purposes only
	rename (abuse_pol abuse_mil) (abuse_police_e abuse_military_e)	
	

//-------//
//-- Apply correction and export table
//-------//

//-- Apply corrections 
	
	global t3vars abuse_police abuse_military abuse_police_e abuse_military_e
	
	mat rownames table3t = $t3vars
	mat rownames table3s = $t3vars

	
	* Add corrected p-values

	* - BH q value (1995)
		bhcorr, mat(table3t) newname(pvalue) outmat(table3ta)
		bhcorr, mat(table3s) newname(pvalue) outmat(table3sa)
	
	* - BH q threshold
		bhcorralt, mat(table3t) newname(pvalue) fdr(0.1) outmat(table3tb)
		bhcorralt, mat(table3s) newname(pvalue) fdr(0.1) outmat(table3sb)
	
	* - Holms (1979)
		holmscorr, mat(table3t) newname(pvalue) fwer(0.1) outmat(table3tc)
		holmscorr, mat(table3s) newname(pvalue) fwer(0.1) outmat(table3sc)

										   
 //-- Add checkmark to the latex table 
 
	checkmark_yes,  outcomes(${t3vars}) mat1t(table3ta) mat1s(table3sa) ///
										mat2t(table3tb) mat2s(table3sb) ///
										mat3t(table3tc) mat3s(table3sc) ename(t3_)

//-- Export the tables
	
	loc var1 `"\textbf{\shortstack{Police \\ abuse}}"'
	loc var2 `"\textbf{\shortstack{Military \\ abuse}}"'

	esttab t3_1 t3_2 t3_3 t3_4 ///
		using "$tables\table_A10.tex", /// 
		legend booktabs f replace b(%9.3fc) se(%9.2fc) ///
		order(1.treatment 2.treatment) compress ///
		indicate("Individual-level controls = ${demovars}" "Block-level controls = ${geovars}" "Neighborhood FE = *.barrio_code", labels("Yes" "No")) ///
		keep(1.treatment 2.treatment) ///
		stats(N r2 c_mean qval qval2 text text1 text2 text3, fmt(0 2 3 3 3) labels("Observations" "\$R^2$" "Control mean" "qval-treatment" "qval-spillover" "BH-treatment" "BH-spillover" "Holm-treatment" "Holm-spillover")) ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var1'"' `"`var2'"')  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   & \multicolumn{2}{c}{\textbf{Monitoring survey}}		   	///
		   & \multicolumn{2}{c}{\textbf{Endline survey}} \\ 			///
		   \cline{2-3} \cline{4-5}	 								///		   
	   \noalign{\smallskip}											///
	   )  															///
	   posthead(\hline) 											///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 								///
		  \end{tabular} 											///
		  \medskip     												///
	   )
	
	

				//-------------------------------------------------//
				//-- 	Table A.11. Treatment effects on abuses  --//
				//	      using stringent correction for  		   //
				//				multiple comparisons 			   //
				//-------------------------------------------------//

//-------//
//-- Run regressions and store p-values 	 
//-------//

//-- Define matrix for multiple hypothesis comparison. P-values.

	mat table3t = J(4,1,.)
	mat table3s = J(4,1,.)

	
//----//								 
//- Regressions with monitoring data	
//----//
				
	use "$data/raw/survey_monitoring.dta", clear
	
	global indiv_control age gender 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	
	
//-- Column 1 (abuses by the police)	
					
	reg abuse_police  i.treatment i.barrio_code ${geovars} ${indiv_control} [pweight=iweight], ///
		vce(cluster manzana_code) baselevels
	
	qui sum abuse_police if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t3_1,  title("Police")
	
	//-- Store p-values
	
	mat table3t[1,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table3s[1,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))
	
	
//-- Column 2 (abuses by the military)

	reg abuse_military  i.treatment i.barrio_code ${geovars} ${indiv_control} [pweight=iweight], ///
		vce(cluster manzana_code) baselevels
	
	qui sum abuse_military if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t3_2,  title("Military")
	
	//-- Store p-values
	
	mat table3t[2,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table3s[2,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))
	


//----//								 
//- Regressions with endline data	
//----//

	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
		
	
//-- Column 3 (abuses by the police)

	reg abuse_police  i.treatment i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
		vce(cluster manzana_code) baselevels
		
	qui sum abuse_police if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t3_3,  title("Police")
	
	//-- Store p-values
	
	mat table3t[3,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table3s[3,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))
		
		
//-- Column 4 (military abuse)

	reg abuse_military  i.treatment i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
		
	qui sum abuse_military if treatment ==0
	estadd scalar c_mean = r(mean), replace
	
	estadd matrix table = r(table)
	estimates store t3_4,  title("Military")
	
	//-- Store p-values
	
	mat table3t[4,1] = 2*ttail(e(df_r), abs(_b[1.treatment]/_se[1.treatment]))
	mat table3s[4,1] = 2*ttail(e(df_r), abs(_b[2.treatment]/_se[2.treatment]))

	
//-- Rename vars for table purposes only
	rename (abuse_pol abuse_mil) (abuse_police_e abuse_military_e)	
	

//-------//
//-- Apply correction and export table
//-------//

//-- Apply corrections 
	
	global t3vars abuse_police abuse_military abuse_police_e abuse_military_e
	
	mat rownames table3t = $t3vars
	mat rownames table3s = $t3vars

	
	* More stringent correction	
		mat it1 = J(rowsof(table3t), colsof(table3t), 1)
		mat is1 = J(rowsof(table3s), colsof(table3s), 2)
		
		mat table3all = (table3t, it1) \ (table3s, is1)
			
	* Add corrected p-values
			
			loc mat "table3"
			
		* Separate matrices. I have to do this in order to append checkmarks to estimated results.
		* I define the following locals and then I separate each matrix after each correction
			loc nt = rowsof(`mat'all)/2
			loc ns = `nt'+1

	
		* - BH q value (1995)
			bhcorr, mat(`mat'all) newname(pvalue) outmat(`mat'alla)
				
				mat `mat'talla = `mat'alla[1..`nt',1...]
				mat `mat'salla = `mat'alla[`ns'...,1...]
		
		* - BH q threshold
			bhcorralt, mat(`mat'all) newname(pvalue) fdr(0.1) outmat(`mat'allb)
								
				mat `mat'tallb = `mat'allb[1..`nt',1...]
				mat `mat'sallb = `mat'allb[`ns'...,1...]
			
		* - Holms (1979)
			holmscorr, mat(`mat'all) newname(pvalue) fwer(0.1) outmat(`mat'allc)
								
				mat `mat'tallc = `mat'allc[1..`nt',1...]
				mat `mat'sallc = `mat'allc[`ns'...,1...]
			
 
//-- Add checkmark to the latex table
	
	checkmark_yes, outcomes(${t3vars}) mat1t(`mat'talla) mat1s(`mat'salla) ///
									   mat2t(`mat'tallb) mat2s(`mat'sallb) ///
									   mat3t(`mat'tallc) mat3s(`mat'sallc) ename(t3_)
						

//-- Export table
	
	loc var1 `"\textbf{\shortstack{Police \\ abuse}}"'
	loc var2 `"\textbf{\shortstack{Military \\ abuse}}"'

	esttab t3_1 t3_2 t3_3 t3_4 ///
		using "$tables\table_A11.tex", /// 
		legend booktabs f replace b(%9.3fc) se(%9.2fc) ///
		order(1.treatment 2.treatment) compress ///
		indicate("Individual-level controls = ${demovars}" "Block-level controls = ${geovars}" "Neighborhood FE = *.barrio_code", labels("Yes" "No")) ///
		keep(1.treatment 2.treatment) ///
		stats(N r2 c_mean qval qval2 text text1 text2 text3, fmt(0 2 3 3 3) labels("Observations" "\$R^2$" "Control mean" "qval-treatment" "qval-spillover" "BH-treatment" "BH-spillover" "Holm-treatment" "Holm-spillover")) ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var1'"' `"`var2'"')  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   & \multicolumn{2}{c}{\textbf{Monitoring survey}}		   	///
		   & \multicolumn{2}{c}{\textbf{Endline survey}} \\ 			///
		   \cline{2-3} \cline{4-5}	 								///		   
	   \noalign{\smallskip}											///
	   )  															///
	   posthead(\hline) 											///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 								///
		  \end{tabular} 											///
		  \medskip     												///
	   )
	

	
			//--------------------------------------------------------//
			//-- 	Table A.12 Treatment effects on violent crime   --//
			//		 victimization and witnessing in survey data	  //
			//--------------------------------------------------------//

//-------//
//-- Run regressions 					
//-------//
					
	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 

	//-- Column 1
	
	reg i2_victimvioldur i.treatment i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i2_victimvioldur if treatment ==0
	estadd scalar c_mean = r(mean), replace 
		
	estadd matrix table = r(table)
	estimates store c_durv2
	
	
	//-- Column 2
	
	reg i2_victimviolpos i.treatment i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i2_victimviolpos if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store c_posv2
	
	
	//-- Column 3
	
	reg i_witnessviol i.treatment i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_witnessviol if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store w_posv
	

//-------//
//-- Run regressions for randomization inference
//-------//

* Define globals for regressions
	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 


* Run regressions

foreach var in i2_victimvioldur i2_victimviolpos i_witnessviol {
	
	di "*** REGRESSIONS WITH VARIABLE `var' ***"
	
	* Create empty matrix
	matrix treat_effects = J(10000, 2, .)
	matrix colnames treat_effects = treat_ef spillover_ef
	
	
	forvalues n = 1/10000{
	
			use "$data/rand_inf/block_simulate_randomizations_p1.dta", clear
				
			di "This is simulation number `n'"
			
		
	//- Obtain treatment status of each block 
			keep manzana treatment_ri_`n'
			rename treatment_ri_`n' treatment_ri
			rename manzana manzana_code
			
	 
	//- Merge with outcomes
			merge 1:m manzana_code using "$data/raw/survey_endline.dta"	
				drop _merge
			
			
	//- Run regressions 
			qui: reg `var' i.treatment_ri i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
			
			* Store coefficients in matrix
			matrix treat_effects[`n', 1] = _b[1.treatment_ri]
			matrix treat_effects[`n', 2] = _b[2.treatment_ri]
}

* Turn ATEs into variables
	
	clear	
	
	svmat treat_effects, names(col)
	
	save "$data/rand_inf/RI_table_A12_`var'.dta", replace
	
}



//------//					 
//-- Store p-values from randomization inference
//------//

loc n = 0	
foreach var in i2_victimvioldur i2_victimviolpos i_witnessviol {
	
	* Load data of simulations
	
	di "*** THIS IS COLUMN NUMBER `var' ***"
	
	use "$data/rand_inf/RI_table_A12_`var'.dta", clear
	loc ++n
	
	
	* Restore estimates of each regression
	
	if `n' == 1 {
		estimates restore c_durv2
	} 
	
	if `n' == 2 {
		estimates restore c_posv2
	} 
	
	if `n' == 3 {
		estimates restore w_posv
	}
	

	* P-value of treatment efffect

	count if abs(treat_ef) > abs(_b[1.treatment])
	estadd scalar p_val_treat =  `r(N)'/10000, replace
	
	* P-value of spillover effect

	count if abs(spillover_ef) > abs(_b[2.treatment])
	estadd scalar p_val_spillover =  `r(N)'/10000, replace
	
} 
					 
					 
//-- Export regressions 
		
	loc var1 `"\shortstack{During \\ intervention}"'
	loc var2 `"\shortstack{After \\ intervention}"'
	loc var3 `"\shortstack{After \\ intervention}"'
		
	esttab c_durv2 c_posv2 w_posv ///
		using "$tables\table_A12.tex", ///
		legend booktabs f replace b(%9.3fc) se(%9.2fc) ///
		order(treatment spillover) compress ///
		indicate("Individual controls = ${demovars}" "Neighborhood FE = *.barrio_code*" "Block-level controls = ${c_geo}" , labels("Yes" "No")) ///
		keep(1.treatment 2.treatment) ///
		stats(N r2 c_mean p_val_treat p_val_spillover, fmt(0 2 3 3 3)  labels("Observations" "\$R^2$" "Control mean" "RI p-value (treatment)" "RI p-value (spillover)")) /// ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var3'"')  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   &  \multicolumn{3}{c}{\textbf{Endline survey}} \\	    	///
		   \cline{2-4}								///
		   &  \multicolumn{2}{c}{\textbf{Victim}}				///
		   &  \textbf{Witness}						\\			///
			  \cline{2-3} \cline{4-4} 						///		   
	   \noalign{\smallskip}									///
	   )  															///
	   posthead(\hline) 															///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 					///
		  \end{tabular} 											///
		  \medskip     												///
	   )
	   

			//------------------------------------------------------------//
			//-- 	Table A.13 Treatment effects on non-violent crime   --//
			//		 victimization and witnessing in survey data	 	  //
			//------------------------------------------------------------//

//-------//
//-- Run regressions 					
//-------//
					
	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 

	
	//-- Column 1 
	
	reg i2_victimnonvioldur i.treatment i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i2_victimnonvioldur if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store c_durnv2
	
	
	//-- Column 2
	
	reg i2_victimnonviolpos i.treatment i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i2_victimnonviolpos if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store c_posnv2
	
	
	//-- Column 3
	
	reg i_witnessnonviol i.treatment i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_witnessnonviol if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store w_posnv
	
					 
//-------//
//-- Run regressions for randomization inference
//-------//

* Define globals for regressions
	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 


* Run regressions

foreach var in i2_victimnonvioldur i2_victimnonviolpos i_witnessnonviol {
	
	di "*** REGRESSIONS WITH VARIABLE `var' ***"
	
	* Create empty matrix
	matrix treat_effects = J(10000, 2, .)
	matrix colnames treat_effects = treat_ef spillover_ef
	
	
	forvalues n = 1/10000{
	
			use "$data/rand_inf/block_simulate_randomizations_p1.dta", clear
				
			di "This is simulation number `n'"
			
		
	//- Obtain treatment status of each block 
			keep manzana treatment_ri_`n'
			rename treatment_ri_`n' treatment_ri
			rename manzana manzana_code
			
	 
	//- Merge with outcomes
			merge 1:m manzana_code using "$data/raw/survey_endline.dta"	
				drop _merge
			
			
	//- Run regressions 
			qui: reg `var' i.treatment_ri i.barrio_code ${demovars} ${geovars} [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
			
			* Store coefficients in matrix
			matrix treat_effects[`n', 1] = _b[1.treatment_ri]
			matrix treat_effects[`n', 2] = _b[2.treatment_ri]
}

* Turn ATEs into variables
	
	clear	
	
	svmat treat_effects, names(col)
	
	save "$data/rand_inf/RI_table_A13_`var'.dta", replace
	
}


//------//					 
//-- Store p-values from randomization inference
//------//

loc n = 0	
foreach var in i2_victimnonvioldur i2_victimnonviolpos i_witnessnonviol {
	
	* Load data of simulations
	
	di "*** THIS IS COLUMN NUMBER `var' ***"
	
	use "$data/rand_inf/RI_table_A13_`var'.dta", clear
	loc ++n
	
	
	* Restore estimates of each regression
	
	if `n' == 1 {
		estimates restore c_durnv2
	} 
	
	if `n' == 2 {
		estimates restore c_posnv2
	} 
	
	if `n' == 3 {
		estimates restore w_posnv
	}
	

	* P-value of treatment efffect

	count if abs(treat_ef) > abs(_b[1.treatment])
	estadd scalar p_val_treat =  `r(N)'/10000, replace
	
	* P-value of spillover effect

	count if abs(spillover_ef) > abs(_b[2.treatment])
	estadd scalar p_val_spillover =  `r(N)'/10000, replace
	
} 
	
	
//-- Export regressions 

	loc var1 `"\shortstack{During \\ intervention}"'
	loc var2 `"\shortstack{After \\ intervention}"'
	loc var3 `"\shortstack{After \\ intervention}"'
		
	esttab c_durnv2 c_posnv2 w_posnv ///
		using "$tables\table_A13.tex", ///
		legend booktabs f replace b(%9.3fc) se(%9.2fc) ///
		order(treatment spillover) compress ///
		indicate("Individual controls = ${demovars}" "Neighborhood FE = *.barrio_code*" "Block-level controls = ${c_geo}" , labels("Yes" "No")) ///
		keep(1.treatment 2.treatment) ///
		stats(N r2 c_mean p_val_treat p_val_spillover, fmt(0 2 3 3 3)  labels("Observations" "\$R^2$" "Control mean" "RI p-value (treatment)" "RI p-value (spillover)")) ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var3'"')  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   &  \multicolumn{3}{c}{\textbf{Endline survey}} \\	    	///
		   \cline{2-4}								///
		   &  \multicolumn{2}{c}{\textbf{Victim}}				///
		   &  \textbf{Witness}						\\			///
			  \cline{2-3} \cline{4-4} 						///		   
	   \noalign{\smallskip}									///
	   )  															///
	   posthead(\hline) 															///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 					///
		  \end{tabular} 											///
		  \medskip     												///
	   )
	   
		
			
			//------------------------------------------------------------//
			//-- 	Table A.14. Heterogeneous treatment effects on      --//
			//		 	crime, crime victimization, and crime 	 		  //	
			//				witnessing by prior crime rate	 	  		  //
			//------------------------------------------------------------//
			
//-- Column 1 --//
	
	//- Load data admin data (during the intervention)
	
	use "$data/raw/admin_data_during.dta", clear
	
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	global blockdemovars block_age block_educ block_pct_male

	
	//- Run regression with crime during intervention 
	
	reg unw_crime2_num  i.treatment##c.prior_unw_crime2_num /// 
		i.barrio_code $geovars $blockdemovars cum_all_unw_crime2_num [pweight=iweight]
	
	qui sum unw_crime2_num if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store ACP
	
	
//-- Column 2 --//

	//- Load admin data (after the intervention)
	
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	global blockdemovars block_age block_educ block_pct_male
			
	use "$data/raw/admin_data_after.dta", clear
	

	//- Run regression with crime after intervention 
	
	reg unw_crime_num  i.treatment##c.prior_unw_crime2_num /// 
		i.barrio_code $geovars $blockdemovars cum_all_unw_crime_num [pweight=iweight]
	
	qui sum unw_crime_num if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store ACPO


//-- Columns 3, 4 y 5--//

	//- Load data of the endline survey

	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 		
		
			
	//- Crime victimization during intervention

	reg i2_victimduringindex_std  i.treatment##c.prior_unw_crime2_num /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i2_victimduringindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store c_dur

					 
	//- Crime victimization  after intervention
	
	reg i2_victimafterindex_std  i.treatment##c.prior_unw_crime2_num /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i2_victimafterindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store c_pos
	

	//- Crime witnessing after intervention 
	
	reg i_witnessindex_std  i.treatment##c.prior_unw_crime2_num /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_witnessindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store w_pos
	

	//-- Export regressions 
	
	loc var1 `"\shortstack{During \\ intervention}"'
	loc var2 `"\shortstack{After \\ intervention}"'
	loc var3 `"\shortstack{During \\ intervention}"'
	loc var4 `"\shortstack{After \\ intervention}"'
	loc var5 `"\shortstack{After \\ intervention}"'
	loc keepvars "1.treatment 2.treatment prior_unw_crime2_num 1.treatment#c.prior_unw_crime2_num 2.treatment#c.prior_unw_crime2_num"
	
	esttab ACP ACPO c_dur c_pos w_pos ///
		using "$tables\table_A14.tex", replace ///
		legend booktabs f  ///
		order(1.treatment 2.treatment) compress ///
		indicate("Individual controls = ${demovars}" "Neighborhood FE = *.barrio_code*" "Block-level controls = ${c_geo}" , labels("Yes" "No")) ///
		keep(`keepvars') ///
		stats(N r2 c_mean, fmt(0 2 3)  labels("Observations" "\$R^2$" "Control mean")) ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var3'"' `"`var4'"' `"`var5'"')  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   &  \multicolumn{2}{c}{\textbf{Admin data}} & 			///
			  \multicolumn{3}{c}{\textbf{Endline survey}} \\	    	///
		   \cline{2-3} \cline{4-6} 								///
		   &  \multicolumn{2}{c}{\textbf{Crime incidence}} &		///
			  \multicolumn{2}{c}{\textbf{Crime victimization}} &	///
			  \multicolumn{1}{c}{\textbf{Crime witnessing}} \\			///
			  \cline{2-3} \cline{4-5} \cline{6-6} 						///		   
	   \noalign{\smallskip}									///
	   )  															///
	   posthead(\hline) 															///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 					///
		  \end{tabular} 											///
		  \medskip     												///
	   )


	   		//------------------------------------------------------------//
			//-- 	Table A.15. Heterogeneous treatment effects on      --//
			//		 	crime victimization and crime witnessing	 	  //	
			//				 by prior crime victimization	 	  		  //
			//------------------------------------------------------------//

//-------//
//-- Run regressions 					
//-------//
					
	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 

	
	//- Crime victimization during intervention

	reg i2_victimduringindex_std  i.treatment##c.i2_victimbeforeindex_std /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i2_victimduringindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store c_dur_cvic2

					 
	//- Crime victimization  after intervention
	
	reg i2_victimafterindex_std  i.treatment##c.i2_victimbeforeindex_std /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i2_victimafterindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store c_pos_cvic2
	

	//- Crime witnessing after intervention 
	
	reg i_witnessindex_std  i.treatment##c.i2_victimbeforeindex_std /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_witnessindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store w_pos_cvic2
	
	
	//-- Export regressions 
	
	loc var1 `"\shortstack{During \\ intervention}"'
	loc var2 `"\shortstack{After \\ intervention}"'
	loc var3 `"\shortstack{During \\ intervention}"'
	loc keepvars "1.treatment 2.treatment i2_victimbeforeindex_std 1.treatment#c.i2_victimbeforeindex_std 2.treatment#c.i2_victimbeforeindex_std"
	
	esttab c_dur_cvic2 c_pos_cvic2 w_pos_cvic2 ///
		using "$tables\table_A15.tex", ///
		legend booktabs f replace b(%9.3fc) se(%9.2fc) ///
		order(1.treatment 2.treatment) compress ///
		indicate("Individual controls = ${demovars}" "Neighborhood FE = *.barrio_code*" "Block-level controls = ${c_geo}" , labels("Yes" "No")) ///
		keep(`keepvars') ///
		stats(N r2 c_mean, fmt(0 2 3)  labels("Observations" "\$R^2$" "Control mean")) ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var3'"' )  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   & \multicolumn{3}{c}{\textbf{Endline survey}} \\	    	///
		   \cline{2-4}  								///
		   &  \multicolumn{2}{c}{\textbf{Crime victimization}} &		///
			  \multicolumn{1}{c}{\textbf{Crime witnessing}} \\			///
			  \cline{2-3} \cline{4-4} 						///		   
	   \noalign{\smallskip}									///
	   )  															///
	   posthead(\hline) 															///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 					///
		  \end{tabular} 											///
		  \medskip     												///
	   )
	   
	      
	   		//------------------------------------------------------------//
			//-- 	Table A.16. Heterogeneous treatment effects on      --//
			//	   crime victimization and crime witnessing  by gender	  //	
			//------------------------------------------------------------//

//-------//
//-- Run regressions 					
//-------//
					
	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 

	
	//- Crime victimization during intervention

	reg i2_victimduringindex_std  i.treatment##i.gender /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i2_victimduringindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store c_dur_gen2

					 
	//- Crime victimization  after intervention
	
	reg i2_victimafterindex_std  i.treatment##i.gender /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i2_victimafterindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store c_pos_gen2
	

	//- Crime witnessing after intervention 
	
	reg i_witnessindex_std  i.treatment##i.gender /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_witnessindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store w_pos_gen2
	
	
	//-- Export regressions 
	
	loc var1 `"\shortstack{During \\ intervention}"'
	loc var2 `"\shortstack{After \\ intervention}"'
	loc var3 `"\shortstack{During \\ intervention}"'

	loc keepvars "1.treatment 2.treatment 1.gender 1.treatment#1.gender 2.treatment#1.gender"
	
	esttab c_dur_gen2 c_pos_gen2 w_pos_gen2 ///
		using "$tables\table_A16.tex", ///
		legend booktabs f replace b(%9.3fc) se(%9.2fc) ///
		order(1.treatment 2.treatment) compress ///
		indicate("Individual controls = ${demovars}" "Neighborhood FE = *.barrio_code*" "Block-level controls = ${c_geo}" , labels("Yes" "No")) ///
		keep(`keepvars') ///
		stats(N r2 c_mean, fmt(0 2 3)  labels("Observations" "\$R^2$" "Control mean")) ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var3'"' )  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   & \multicolumn{3}{c}{\textbf{Endline survey}} \\	    	///
		   \cline{2-4}  								///
		   &  \multicolumn{2}{c}{\textbf{Crime victimization}} &		///
			  \multicolumn{1}{c}{\textbf{Crime witnessing}} \\			///
			  \cline{2-3} \cline{4-4} 						///		   
	   \noalign{\smallskip}									///
	   )  															///
	   posthead(\hline) 															///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 					///
		  \end{tabular} 											///
		  \medskip     												///
	   )   
	   

		   	//------------------------------------------------------------//
			//-- 	Table A.17. Heterogeneous treatment effects on      --//
			//	   	  perceptions of safety by prior crime rate	  		  //	
			//------------------------------------------------------------//

//-------//
//-- Run regressions 					
//-------//
					
	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 

	
	//- All safety 

	reg i_securityallindex_std  i.treatment##c.prior_unw_crime2_nu /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_securityallindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store sty1_priorcrime

					 
	//- Personal safety
	
	reg i_securityallindex_ps  i.treatment##c.prior_unw_crime2_nu /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_securityallindex_ps if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store sty2_priorcrime
	

	//- Business safety 
	
	reg i_business  i.treatment##c.prior_unw_crime2_nu /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_business if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store sty3_priorcrime
	
	
	//-- Export regressions 
	
	loc var1 `"\textbf{\shortstack{All \\ safety}}"'
	loc var2 `"\textbf{\shortstack{Personal \\ safety}}"'
	loc var3 `"\textbf{\shortstack{Business \\ safety}}"'
	loc keepvars "1.treatment 2.treatment prior_unw_crime2_num 1.treatment#c.prior_unw_crime2_num 2.treatment#c.prior_unw_crime2_num"

	esttab sty1_priorcrime sty2_priorcrime sty3_priorcrime ///
		using "$tables\table_A17.tex", ///
		legend booktabs f replace b(%9.3fc) se(%9.2fc) ///
		order(treatment spillover) compress ///
		indicate("Individual controls = ${demovars}" "Neighborhood FE = *.barrio_code*" "Block-level controls = ${c_geo}" , labels("Yes" "No")) ///
		keep(`keepvars') ///
		stats(N r2 c_mean, fmt(0 2 3)  labels("Observations" "\$R^2$" "Control mean")) ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var3'"' )  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   &  \multicolumn{3}{c}{\textbf{Endline survey}} \\	    	///
		   \cline{2-4}  								///
	   )  															///
	   posthead(\hline) 															///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 					///
		  \end{tabular} 											///
		  \medskip     												///
	   ) 
	   
	
		   	//------------------------------------------------------------//
			//-- 	Table A.18. Heterogeneous treatment effects on      --//
			//	   perceptions of safety by prior crime victimization	  //	
			//------------------------------------------------------------//

//-------//
//-- Run regressions 					
//-------//
					
	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 

	
	//- All safety 

	reg i_securityallindex_std  i.treatment##c.i2_victimbeforeindex_std /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_securityallindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store sty1_cvic

					 
	//- Personal safety
	
	reg i_securityallindex_ps  i.treatment##c.i2_victimbeforeindex_std /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_securityallindex_ps if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store sty2_cvic
	

	//- Business safety 
	
	reg i_business  i.treatment##c.i2_victimbeforeindex_std /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_business if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store sty3_cvic
	
	
//----//								 
//-- Export regressions 	
//----//
	
	loc var1 `"\textbf{\shortstack{All \\ safety}}"'
	loc var2 `"\textbf{\shortstack{Personal \\ safety}}"'
	loc var3 `"\textbf{\shortstack{Business \\ safety}}"'
	loc keepvars "1.treatment 2.treatment i2_victimbeforeindex_std 1.treatment#c.i2_victimbeforeindex_std 2.treatment#c.i2_victimbeforeindex_std"

	esttab sty1_cvic sty2_cvic sty3_cvic ///
		using "$tables\table_A18.tex", ///
		legend booktabs f replace b(%9.3fc) se(%9.2fc) ///
		order(treatment spillover) compress ///
		indicate("Individual controls = ${demovars}" "Neighborhood FE = *.barrio_code*" "Block-level controls = ${c_geo}" , labels("Yes" "No")) ///
		keep(`keepvars') ///
		stats(N r2 c_mean, fmt(0 2 3)  labels("Observations" "\$R^2$" "Control mean")) ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var3'"' )  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   &  \multicolumn{3}{c}{\textbf{Endline survey}} \\	    	///
		   \cline{2-4}  								///
	   )  															///
	   posthead(\hline) 															///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 					///
		  \end{tabular} 											///
		  \medskip     												///
	   ) 
	   
	
		   	//------------------------------------------------------------//
			//-- 	Table A.19. Heterogeneous treatment effects on      --//
			//	   			perceptions of safety by gender	 			  //	
			//------------------------------------------------------------//

//-------//
//-- Run regressions 					
//-------//
					
	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 

	
	//- All safety 

	reg i_securityallindex_std  i.treatment##i.gender /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_securityallindex_std if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store sty1_gen

					 
	//- Personal safety
	
	reg i_securityallindex_ps  i.treatment##i.gender /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_securityallindex_ps if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store sty2_gen
	

	//- Business safety 
	
	reg i_business  i.treatment##i.gender /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum i_business if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store sty3_gen
	
	
//----//								 
//-- Export regressions 	
//----//
	
	loc var1 `"\textbf{\shortstack{All \\ safety}}"'
	loc var2 `"\textbf{\shortstack{Personal \\ safety}}"'
	loc var3 `"\textbf{\shortstack{Business \\ safety}}"'
	local keepvars "1.treatment 2.treatment 1.gender 1.treatment#1.gender 2.treatment#1.gender"

	esttab sty1_gen sty2_gen sty3_gen ///
		using "$tables\table_A19.tex", ///
		legend booktabs f replace b(%9.3fc) se(%9.2fc) ///
		order(treatment spillover) compress ///
		indicate("Individual controls = ${demovars}" "Neighborhood FE = *.barrio_code*" "Block-level controls = ${c_geo}" , labels("Yes" "No")) ///
		keep(`keepvars') ///
		stats(N r2 c_mean, fmt(0 2 3)  labels("Observations" "\$R^2$" "Control mean")) ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var3'"' )  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   &  \multicolumn{3}{c}{\textbf{Endline survey}} \\	    	///
		   \cline{2-4}  								///
	   )  															///
	   posthead(\hline) 															///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 					///
		  \end{tabular} 											///
		  \medskip     												///
	   ) 	  
	
	   
		   	//--------------------------------------------------------//
			//-- 	Table A.20. Heterogeneous treatment effects     --//
			//	   	 	on abuses by prior crime rate	 			  //	
			//--------------------------------------------------------//

//-------//
//-- Run regressions with monitoring data 					
//-------//
					
	use "$data/raw/survey_monitoring.dta", clear

	global indiv_control age gender 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	
	
	//- Abuse police  

	reg abuse_police  i.treatment##c.prior_unw_crime2_num /// 
			i.barrio_code ${indiv_control} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum abuse_police if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store t3_1_priorcrime

	
	//- Abuse military   

	reg abuse_military  i.treatment##c.prior_unw_crime2_num /// 
			i.barrio_code ${indiv_control} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum abuse_military if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store t3_2_priorcrime

			
//-------//
//-- Run regressions with endline data 					
//-------//
					
	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 

	
	//- Abuse police  

	reg abuse_police  i.treatment##c.prior_unw_crime2_num /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum abuse_police if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store t3_3_priorcrime

	
	//- Abuse military   

	reg abuse_military  i.treatment##c.prior_unw_crime2_num /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum abuse_military if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store t3_4_priorcrime

	
//----//								 
//-- Export regressions 	
//----//
	
	label var prior_unw_crime2_num "Prior crime"
	loc var1 `"\textbf{\shortstack{Police \\ abuse}}"'
	loc var2 `"\textbf{\shortstack{Military \\ abuse}}"'
	loc var3 `"\textbf{\shortstack{Police \\ abuse}}"'
	loc var4 `"\textbf{\shortstack{Military \\ abuse}}"'
	loc keepvars "1.treatment 2.treatment prior_unw_crime2_num 1.treatment#c.prior_unw_crime2_num 2.treatment#c.prior_unw_crime2_num"

	esttab t3_1_priorcrime t3_2_priorcrime t3_3_priorcrime t3_4_priorcrime ///
		using "$tables/table_A20.tex", ///
		legend booktabs f replace b(%9.3fc) se(%9.2fc) ///
		order(treatment spillover) compress ///
		indicate("Individual controls = ${demovars}" "Neighborhood FE = *.barrio_code*" "Block-level controls = ${c_geo}" , labels("Yes" "No")) ///
		keep(`keepvars') ///
		stats(N r2 c_mean, fmt(0 2 3)  labels("Observations" "\$R^2$" "Control mean")) ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var3'"' `"`var4'"')  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   &  \multicolumn{2}{c}{\textbf{Monitoring survey}} &    	///
			  \multicolumn{2}{c}{\textbf{Endline survey}} \\      		///
		   \cline{2-3} \cline{4-5} 									///
	   )  															///
	   posthead(\hline) 															///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 					///
		  \end{tabular} 											///
		  \medskip     												///
	   ) 	  
	   
			
		   	//--------------------------------------------------------//
			//-- 	Table A.21. Heterogeneous treatment effects     --//
			//	   	  on abuses by prior crime victimization	 	  //	
			//--------------------------------------------------------//
			
//-------//
//-- Run regressions with endline data 					
//-------//
					
	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	
	
	//- Abuse police  

	reg abuse_police  i.treatment##c.i2_victimbeforeindex_std /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum abuse_police if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store t3_3_cvic

	
	//- Abuse military   

	reg abuse_military  i.treatment##c.i2_victimbeforeindex_std /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum abuse_military if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store t3_4_cvic

	
//----//								 
//-- Export regressions 	
//----//

	loc var1 `"\textbf{\shortstack{Police \\ abuse}}"'
	loc var2 `"\textbf{\shortstack{Military \\ abuse}}"'
	loc keepvars "1.treatment 2.treatment i2_victimbeforeindex_std 1.treatment#c.i2_victimbeforeindex_std 2.treatment#c.i2_victimbeforeindex_std"

	esttab t3_3_cvic t3_4_cvic ///
		using "$tables\table_A21.tex", ///
		legend booktabs f replace b(%9.3fc) se(%9.2fc) ///
		order(treatment spillover) compress ///
		indicate("Individual controls = ${demovars}" "Neighborhood FE = *.barrio_code*" "Block-level controls = ${c_geo}" , labels("Yes" "No")) ///
		keep(`keepvars') ///
		stats(N r2 c_mean, fmt(0 2 3)  labels("Observations" "\$R^2$" "Control mean")) ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"')  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   &  \multicolumn{2}{c}{\textbf{Endline survey}} \\      		///
		   \cline{2-3} 									///
	   )  															///
	   posthead(\hline) 															///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 					///
		  \end{tabular} 											///
		  \medskip     												///
	   )
	   

	   		//-----------------------------------------------//
			//-- 	Table A.22. Heterogeneous treatment    --//
			//	   	 	effects on abuses by gender	 	     //	
			//-----------------------------------------------//

//-------//
//-- Run regressions with monitoring data 					
//-------//
					
	use "$data/raw/survey_monitoring.dta", clear

	global indiv_control age gender 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 
	
	
	//- Abuse police  

	reg abuse_police  i.treatment##i.gender /// 
			i.barrio_code ${indiv_control} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum abuse_police if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store t3_1_gender

	
	//- Abuse military   

	reg abuse_military  i.treatment##i.gender /// 
			i.barrio_code ${geovars} ${indiv_control} [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum abuse_military if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store t3_2_gender 

			
//-------//
//-- Run regressions with endline data 					
//-------//
					
	use "$data/raw/survey_endline.dta", clear

	global demovars age gender educ 
	global geovars number_buildings_sampling area bat_min cai_min ptr_min 

	
	//- Abuse police  

	reg abuse_police  i.treatment##i.gender /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum abuse_police if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store t3_3_gender

	
	//- Abuse military   

	reg abuse_military  i.treatment##i.gender /// 
			i.barrio_code ${demovars} ${geovars}  [pweight=iweight], ///
			vce(cluster manzana_code) baselevels
	
	qui sum abuse_military if treatment ==0
	estadd scalar c_mean = r(mean), replace 
	
	estadd matrix table = r(table)
	estimates store t3_4_gender 

			
//----//								 
//-- Export regressions 	
//----//

	loc var1 `"\textbf{\shortstack{Police \\ abuse}}"'
	loc var2 `"\textbf{\shortstack{Military \\ abuse}}"'
	loc var3 `"\textbf{\shortstack{Police \\ abuse}}"'
	loc var4 `"\textbf{\shortstack{Military \\ abuse}}"'
	local keepvars "1.treatment 2.treatment 1.gender 1.treatment#1.gender 2.treatment#1.gender"

	esttab t3_1_gender t3_2_gender t3_3_gender t3_4_gender ///
		using "$tables\table_A22.tex", ///
		legend booktabs f replace b(%9.3fc) se(%9.2fc) ///
		order(treatment spillover) compress ///
		indicate("Individual controls = ${demovars}" "Neighborhood FE = *.barrio_code*" "Block-level controls = ${c_geo}" , labels("Yes" "No")) ///
		keep(`keepvars') ///
		stats(N r2 c_mean, fmt(0 2 3)  labels("Observations" "\$R^2$" "Control mean")) ///
		nostar	///
		cells("b(fmt(3) )" "ci_l(par) & ci_u(par)" "p(fmt(3) par)") incelldelimiter(", ") ///
		label nonum nodepvar nogaps mtitle(`"`var1'"' `"`var2'"' `"`var3'"' `"`var4'"')  ///
		collabels(none) ///
		nolines														///
		prehead( 													///
		   \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}				///
		   \begin{tabular}{@{\extracolsep{4pt}}l*{@M}{c}@{}}    	/// 
		   \noalign{\smallskip} 									///
		   &  \multicolumn{2}{c}{\textbf{Monitoring survey}} &    	///
			  \multicolumn{2}{c}{\textbf{Endline survey}} \\      		///
		   \cline{2-3} \cline{4-5} 									///
	   )  															///
	   posthead(\hline) 															///
	   postfoot(  													///
		  \noalign{\smallskip} \hline 					///
		  \end{tabular} 											///
		  \medskip     												///
	   ) 	  
	  
				
	
	
	
	